我是React的新手。我不确定从哪里开始。尝试发布时收到以下错误:代理错误:无法将请求/ api / user从localhost:3000代理到http://localhost:3000/。 [1]有关更多信息,请参见https://nodejs.org/api/errors.html#errors_common_system_errors(ECONNRESET)。
这是我的server.js文件:
var cors = require("cors");
var express = require("express");
var bodyParser = require("body-parser");
var methodOverride = require("method-override");
var PORT = process.env.PORT || 3001;
var app = express();
var Sequelize = require("Sequelize");
var passport = require("passport");
var session = require("express-session");
var env = require("dotenv").load();
const routes = require("./routes");
// Requiring our models for syncing
var db = require("./models");
// Add routes, both API and view
app.use(routes);
// Creates connection with Sequelize
const connection = new Sequelize("live_work_chill_db", "root", "password", {
host: "localhost",
dialect: "mysql",
operatorsAliases: false
});
// Create instance of cors with Express
app.use(cors());
app.use(bodyParser.json());
app.use(express.static(process.cwd() + "/public"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride("_method"));
// Initialize passport, express session and passport session
app.use(
session({
secret: process.env.PASSPORT_SECRET,
resave: true,
saveUninitialized: true
})
); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
// // Routes - Import our auth.js file
// var authRoute = require('./controllers/auth.js')(app,passport); // Added passport as an argument to pass its functionality to auth.js
// load passport strategies
require("./config/passport/passport.js")(passport, db.Users);
// Syncing our sequelize models and then starting our express app
db.sequelize.sync({ force: false }).then(function() {
app.listen(PORT, function() {
console.log("App listening on PORT " + PORT);
});
});
这是我的package.json文件:
{
"name": "mern",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "node server.js",
"start:dev": "concurrently \"nodemon server.js\" \"npm run client\"",
"client": "cd client && npm run start",
"install": "cd client && npm install",
"build": "cd client && npm run build",
"heroku-postbuild": "npm run build"
},
"author": "",
"license": "ISC",
"devDependencies": {
"concurrently": "^4.1.0",
"nodemon": "^1.18.7"
},
"dependencies": {
"axios": "^0.18.0",
"bcrypt-nodejs": "^0.0.3",
"cors": "^2.8.5",
"express": "^4.16.3",
"express-session": "^1.15.6",
"if-env": "^1.0.4",
"method-override": "^3.0.0",
"mysql2": "^1.6.4",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"react": "^16.8.1",
"react-router-dom": "^4.3.1",
"react-scripts": "^2.1.3",
"sequelize": "^4.42.0"
}
}
答案 0 :(得分:0)
尝试将其添加到package.json
"proxy": "http://localhost:3000",