错误[err_http_headers_sent] Express Server在Node.js中崩溃
因此,此代码正常工作并且可以完成所有操作,但是继续崩溃我的Express Server。 这是错误: 节点:内部/错误:464 ERRORCAPTURESTACKTRACE(ERR); ^
错误[err_http_headers_sent]:发送给客户端后无法设置标头 在New Nodeerror(节点:内部/错误:371:5) 在serverresponse.setheader(节点:_http_outonodoing:576:11) 在serverresponse.header(c:\ users \ user \ user \ dragonfitness \ crud-application \ server \ node_modules \ express \ express \ lib \ response.js:776:10) 在serverresponse.send(c:\ users \ user \ user \ dragonfitness \ crud-application \ server \ node_modules \ express \ express \ lib \ response.js:170:12) 在serverresponse.json(c:\ users \ user \ user \ dragonfitness \ crud-application \ server \ node_modules \ express \ express \ lib \ response.js:267:15) 在serverresponse.send(c:\ users \ user \ user \ dragonfitness \ crud-application \ server \ node_modules \ express \ express \ lib \ response.js:158:21) 应要求。 (c:\ user \ user \ dragonfitness \ crud-application \ server \ index.js:89:11){ 代码:'err_http_headers_sent'console.logs
app.post('/login', async (req, res)=>{
try{
var sql = "SELECT password FROM dbo.user_info WHERE user_name = @username"
const request = new Request(sql,
(err, result) => {
if (err) {
console.error(err.message)
}
if(result == 0){
console.log("failed")
return res.send({message: "Wrong username/password combination"})
}
}
);
request.on('row', async function(columns) {
console.log(columns)
const password =columns[0].value;
console.log(req.body.passwordlog)
console.log(password)
const isValid = await bcrypt.compare(req.body.passwordlog, password)
if(isValid){
console.log("passed")
return res.send({message: "Login Sucessful"})
}
});
request.addParameter('username', TYPES.NVarChar, req.body.usernamelog)
connection.execSql(request);
}catch{
console.log("better not")
return res.status(500).send()
}
});
正在完美工作,并将请求发送回前端很好。并执行该操作,但它使我的Express Server崩溃了。有帮助吗?
So this code works fine and does everything, but continues to crash my express server.
This is the error:
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:371:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at ServerResponse.header (C:\Users\user\dragonfitness\crud-application\server\node_modules\express\lib\response.js:776:10)
at ServerResponse.send (C:\Users\user\dragonfitness\crud-application\server\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\user\dragonfitness\crud-application\server\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (C:\Users\user\dragonfitness\crud-application\server\node_modules\express\lib\response.js:158:21)
at Request. (C:\Users\user\dragonfitness\crud-application\server\index.js:89:11) {
code: 'ERR_HTTP_HEADERS_SENT'
app.post('/login', async (req, res)=>{
try{
var sql = "SELECT password FROM dbo.user_info WHERE user_name = @username"
const request = new Request(sql,
(err, result) => {
if (err) {
console.error(err.message)
}
if(result == 0){
console.log("failed")
return res.send({message: "Wrong username/password combination"})
}
}
);
request.on('row', async function(columns) {
console.log(columns)
const password =columns[0].value;
console.log(req.body.passwordlog)
console.log(password)
const isValid = await bcrypt.compare(req.body.passwordlog, password)
if(isValid){
console.log("passed")
return res.send({message: "Login Sucessful"})
}
});
request.addParameter('username', TYPES.NVarChar, req.body.usernamelog)
connection.execSql(request);
}catch{
console.log("better not")
return res.status(500).send()
}
});
the console.logs are working perfect and sends the request back to the front end just fine. and does what it's suppose to but it crashes my express server. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
,
您正在尝试多次发送响应。将返回申请到每个res.send()语句,它可以正常工作。
and
You are trying to send response multiple times. Apply return to each and every res.send() statement and it will work fine.