PUT 请求后节点 js 服务器崩溃
Node js server crashes after PUT request
发出放置请求后,应用程序因错误而崩溃
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
(node:21666) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(Use `node --trace-deprecation ...` to show where the warning was created)
Server up and running
(node:21666) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify
node:internal/process/promises:227
triggerUncaughtException(err, true /* fromPromise */);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:329:5)
at ServerResponse.setHeader (node:_http_outgoing:572:11)
at ServerResponse.header (/Users/kayode/Desktop/learn-mern/auth/node_modules/express/lib/response.js:771:10)
at ServerResponse.json (/Users/kayode/Desktop/learn-mern/auth/node_modules/express/lib/response.js:264:10)
at ServerResponse.send (/Users/kayode/Desktop/learn-mern/auth/node_modules/express/lib/response.js:158:21)
at /Users/kayode/Desktop/learn-mern/auth/routes/users.js:36:24
at processTicksAndRejections (node:internal/process/task_queues:94:5) {
code: 'ERR_HTTP_HEADERS_SENT'
}
[nodemon] app crashed - waiting for file changes before starting...
这是我的代码...
const router = require('express').Router();
const User = require('../models/User')
router.put('/:email', async (req, res, next) => {
res.send(req.params._id)
let email = req.params.email;
let user = {
name: req.body.name,
email: req.body.email,
password: req.body.password
}
try {
await User.findOneAndUpdate(email, user);
res.status(200).send(JSON.stringify('200'));
console.log('Here first')
} catch (err) {
res.status(400).send(err) //not commented
}
})
module.exports = router;
但是,如果我尝试发出另一个请求(使用邮递员),没有刷新服务器,它说发送了一个错误无法发送请求
但是话又说回来,在第 36 行,“在 /Users/kayode/Desktop/learn-mern/auth/routes/users.js:36:24”,其中写着 res.status(400).send(err) //not commented
如果我注释掉该行(即不是 return 错误),PUT 请求将会成功,之后的另一个请求也可以在不刷新或重新启动服务器的情况下工作。
我知道我可以只注释掉那一行然后继续,但我需要知道发生了什么,谢谢!
您正在请求的开头发送响应,并尝试在请求的末尾再次发送(第 5 行)。
const router = require('express').Router();
const User = require('../models/User')
router.put('/:email', async (req, res, next) => {
let email = req.params.email;
let user = {
name: req.body.name,
email: req.body.email,
password: req.body.password
}
try {
await User.findOneAndUpdate(email, user);
res.status(200).send(JSON.stringify('200'));
console.log('Here first')
} catch (err) {
res.status(400).send(err) //not commented
}
})
module.exports = router;
发出放置请求后,应用程序因错误而崩溃
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
(node:21666) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(Use `node --trace-deprecation ...` to show where the warning was created)
Server up and running
(node:21666) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify
node:internal/process/promises:227
triggerUncaughtException(err, true /* fromPromise */);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:329:5)
at ServerResponse.setHeader (node:_http_outgoing:572:11)
at ServerResponse.header (/Users/kayode/Desktop/learn-mern/auth/node_modules/express/lib/response.js:771:10)
at ServerResponse.json (/Users/kayode/Desktop/learn-mern/auth/node_modules/express/lib/response.js:264:10)
at ServerResponse.send (/Users/kayode/Desktop/learn-mern/auth/node_modules/express/lib/response.js:158:21)
at /Users/kayode/Desktop/learn-mern/auth/routes/users.js:36:24
at processTicksAndRejections (node:internal/process/task_queues:94:5) {
code: 'ERR_HTTP_HEADERS_SENT'
}
[nodemon] app crashed - waiting for file changes before starting...
这是我的代码...
const router = require('express').Router();
const User = require('../models/User')
router.put('/:email', async (req, res, next) => {
res.send(req.params._id)
let email = req.params.email;
let user = {
name: req.body.name,
email: req.body.email,
password: req.body.password
}
try {
await User.findOneAndUpdate(email, user);
res.status(200).send(JSON.stringify('200'));
console.log('Here first')
} catch (err) {
res.status(400).send(err) //not commented
}
})
module.exports = router;
但是,如果我尝试发出另一个请求(使用邮递员),没有刷新服务器,它说发送了一个错误无法发送请求
但是话又说回来,在第 36 行,“在 /Users/kayode/Desktop/learn-mern/auth/routes/users.js:36:24”,其中写着 res.status(400).send(err) //not commented
如果我注释掉该行(即不是 return 错误),PUT 请求将会成功,之后的另一个请求也可以在不刷新或重新启动服务器的情况下工作。
我知道我可以只注释掉那一行然后继续,但我需要知道发生了什么,谢谢!
您正在请求的开头发送响应,并尝试在请求的末尾再次发送(第 5 行)。
const router = require('express').Router();
const User = require('../models/User')
router.put('/:email', async (req, res, next) => {
let email = req.params.email;
let user = {
name: req.body.name,
email: req.body.email,
password: req.body.password
}
try {
await User.findOneAndUpdate(email, user);
res.status(200).send(JSON.stringify('200'));
console.log('Here first')
} catch (err) {
res.status(400).send(err) //not commented
}
})
module.exports = router;