Проблемы с прокси Cors на сервере. js файл (реагирует, express, sql) - PullRequest
0 голосов
/ 07 января 2020

Я создал базовый c сайт с полным стеком, используя ms sql и express. Первоначально маршруты get работали, но после реализации почтового маршрута они прекратились.

Я полагаю, что получаю ошибку cors.

Proxy error: Could not proxy request /admin-view-users from localhost:3000 to http://localhost:5000/.
[1] See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

мой сервер. js

const express = require("express");
const sql = require("mssql");
var cors = require("cors");
const path = require("path");
var bodyParser = require("body-parser");
const port = process.env.PORT || 5000;

const app = express();
app.use(cors());

app.use(express.json());

const config = {
  user: "sas",
  password: "Mypassword456",
  server: "DEVSQL_2014", // You can use 'localhost\\instance' to connect to named instance
  database: "TestDBWebsite"
};

//AdminView users just pulls the users from the database

app.get("/admin-view-users", cors(), function(req, res) {
  // connect to your database
  sql.connect(config, function(err) {
    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    request.query("select * from Users2 ", function(err, recordset) {
      if (err) console.log(err);

      // send records as a response

      res.json(recordset);

      res.end();
    });
  });
});

app.get("/admin-view-users", function(req, res) {
  // connect to your database
  sql.connect(config, function(err) {
    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    request.query("select * from Users2 ", function(err, recordset) {
      if (err) console.log(err);

      // send records as a response

      res.json(recordset);

      res.end();
    });
  });
});

app.get("/user-questions", function(req, res) {
  // connect to your database
  sql.connect(config, function(err) {
    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    request.query("select * from Questions ", function(err, recordset) {
      if (err) console.log(err);

      // send records as a response

      res.json(recordset);
    });
  });
});

//

var jsonParser = bodyParser.json();

app.use(express.bodyParser());

app.post("/admin-Add-Users", jsonParser, function(request, response) {
  var email = req.body.email;
  var password = req.body.password;

  var request = new sql.Request();
  // query to the database and get the records

  console.log(email, password); // your JSON
  // echo the result back
  console.log(request.body);

  request.query(
    "insert into Login (email, password) values ('" +
      email +
      "','" +
      password +
      "')",
    function(err, recordset) {
      if (err) console.log(err);
    }
  );
  response.send({ message: "Success" });
});

app.listen(port, () => `Server running on port ${port}`);

Я включил "app.use (cors ());» который я предположил бы решить это, но это не так.

Пожалуйста, дайте совет, если можете.

1 Ответ

1 голос
/ 07 января 2020

Первое, что приходит мне в голову, - это двойное использование CORS.

Вы помещаете его в стек промежуточного программного обеспечения и затем снова вызываете его здесь:

app.get("/admin-view-users", cors(), function(req, res) {

Пожалуйста, попробуйте использовать это только один раз:

https://www.npmjs.com/package/cors

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...