错误[err_http_headers_sent] Express Server在Node.js中崩溃

发布于 2025-01-23 18:32:31 字数 2036 浏览 0 评论 0原文

因此,此代码正常工作并且可以完成所有操作,但是继续崩溃我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2025-01-30 18:32:31

返回res.send()eveywhere

 return res.send({message: "Wrong username/password combination"})

return res.send({message: "Login Sucessful"})

您正在尝试多次发送响应。将返回申请到每个res.send()语句,它可以正常工作。

Return res.send() eveywhere

 return res.send({message: "Wrong username/password combination"})

and

return res.send({message: "Login Sucessful"})

You are trying to send response multiple times. Apply return to each and every res.send() statement and it will work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文