我正在本地运行MERN应用程序,每当尝试使用错误的密码登录时,都会不断收到错误消息。我可以使用正确的凭据登录而不会出现任何错误。
这是我的客户端 package.json 文件:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"axios": "^0.19.2",
"materialize-css": "^1.0.0-rc.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-redux": "^7.2.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"redux": "^4.0.5",
"redux-promise": "^0.6.0",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"http-proxy-middleware": "^0.19.1"
}
}
里面的setupProxy.js 文件:
const proxy = require("http-proxy-middleware");
// module.exports = function(app) {
// app.use(proxy("/api", { target: "http://localhost:5000/" }));
// };
module.exports = function(app) {
app.use(
proxy("/api", {
target: "http://localhost:5000/",
headers: {
Connection: "keep-alive"
}
})
);
};
服务器 package.json 文件:
{
"name": "simple-dentfx-app",
"version": "1.0.0",
"description": "MERN dentfx app",
"main": "index.js",
"engine": {
"node": "10.13.0",
"npm": "6.13.4"
},
"scripts": {
"start": "node server/index.js",
"backend": "nodemon server/index.js",
"frontend": "npm run start --prefix client",
"dev": "concurrently \"npm run backend\" \"npm run start --prefix client\""
},
"author": "Enes Tatli",
"license": "ISC",
"dependencies": {
"bcrypt": "^3.0.8",
"body-parser": "^1.18.3",
"cookie-parser": "^1.4.3",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.4.20"
},
"devDependencies": {
"concurrently": "^5.1.0",
"nodemon": "^2.0.2"
}
}
服务器 * index.js 文件:
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const { User } = require("./models/user");
const config = require("./config/key");
const { auth } = require("./middleware/auth");
mongoose
.connect(
config.mongoURI,
{ useNewUrlParser: true }
)
.then(() => console.log("DB connected"))
.catch(err => console.log(err));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.get("/", (req, res) => {
res.json({ Hello: "I am happy to deploy our application" });
});
app.get("/api/users/auth", auth, (req, res) => {
res.status(200).json({
_id: req._id,
isAuth: true,
email: req.user.email,
name: req.user.name,
lastName: req.user.lastName,
role: req.user.role
});
});
app.post("/api/users/register", (req, res) => {
const user = new User(req.body);
user.save((err, userData) => {
if (err) return res.json({ success: false, err });
return res.status(200).json({
success: true
});
});
});
app.post("/api/users/login", (req, res) => {
//find the email
User.findOne({ email: req.body.email }, (err, user) => {
if (!user)
return res.json({
loginSuccess: false,
message: "Auth failed, email not found"
});
//comparePassword
user.comparePassword(req.body.password, (err, isMatch) => {
if (!isMatch) {
return res.json({ loginSuccess: false, message: "Wrong Password" });
}
});
//generateToken
user.generateToken((err, user) => {
if (err) return res.status(400).send(err);
res
.cookie("x_auth", user.token)
.status(200)
.json({
loginSuccess: true
});
});
});
});
app.get("/api/users/logout", auth, (req, res) => {
User.findOneAndUpdate({ _id: req.user._id }, { token: "" }, (err, doc) => {
if (err) return res.json({ success: false, err });
return res.status(200).send({
success: true
});
});
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server Running at ${port}`);
});
最后是错误消息的打印输出:
Server Running at 5000
[0] (node:15816) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
[0] DB connected
[0] _http_outgoing.js:470
[0] throw new ERR_HTTP_HEADERS_SENT('set');
[0] ^
[0]
[0] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at ServerResponse.setHeader (_http_outgoing.js:470:11)
...
[0] [nodemon] app crashed - waiting for file changes before starting...
[1] [HPM] Error occurred while trying to proxy request /api/users/login from localhost:3000 to http://localhost:5000/ (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)```