【问题标题】:Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - Express + Request错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头 - Express + Request
【发布时间】:2019-04-14 09:13:55
【问题描述】:

我知道这是一个非常基本的问题,但我发现自己花了太多时间来理解为什么会发生这种情况。

下面是我使用 npm request lib 来查询 api 的 api,我用快速响应发送响应。当我点击这个时,我得到:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (C:\Users\GayathriGanesan\Documents\sampleNode\node_modules\express\lib\response.js:767:10)
    at ServerResponse.send (C:\Users\GayathriGanesan\Documents\sampleNode\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (C:\Users\GayathriGanesan\Documents\sampleNode\node_modules\express\lib\response.js:267:15)
    at Request._callback (C:\Users\GayathriGanesan\Documents\sampleNode\app.js:80:34)
    at Request.self.callback (C:\Users\GayathriGanesan\Documents\sampleNode\node_modules\request\request.js:185:22)
    at Request.emit (events.js:189:13)
    at Request.<anonymous> (C:\Users\GayathriGanesan\Documents\sampleNode\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:189:13)
    at IncomingMessage.<anonymous> (C:\Users\GayathriGanesan\Documents\sampleNode\node_modules\request\request.js:1083:12)

下面是编写的代码。

     var express = require('express');
     var app = express();
     var request=require("request");
    const bodyParser = require('body-parser');
    app.use(bodyParser.json());
     app.use(function(req, res, next) {
            res.header("Access-Control-Allow-Origin", "*");
            res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,    Content-Type, Accept,clientSecret");
            res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,PATCH,DELETE,OPTIONS');

            next();
      });

      app.post("/push/v1/apps/:appID/devices",function(req,response){
                var appID=req.params.appID;
            var options={
                url:"https://pushapp.sampleapp.net/push/v1/apps/"+appID+"/devices",
                headers:{
                    'Content-Type':req.headers["content-type"],
                    'clientSecret':req.headers["clientsecret"]
                },
                body: JSON.stringify(req.body)
            }
               request.post(options,function(err,res,body){
                if(res.statusCode==201){  
                    response.sendStatus(201).json(JSON.parse(body));
                }
                else{
                    response.sendStatus(res.statusCode);
                }

            });

            });

请您帮助理解原因。我可以以某种方式猜测回调发生了两次。但不确定如何。

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    问题在于res.sendStatus 设置了给定的响应 HTTP 状态代码并将其字符串表示形式作为响应正文发送(请参阅documentation)。 res.json 将设置 content-type 响应头,但此时响应已经发送到客户端。

    因此,您可以简单地使用res.status,而不是使用res.sendStatus

    response.status(201).json(JSON.parse(body));
    

    【讨论】:

    • 但我发现状态已被弃用。
    • 好像不是这样:github.com/expressjs/express/blob/…你从哪里读到的?
    • 嘿..对不起。我想我对 res.send(Status) 感到困惑。感谢您的投入。它确实对我帮助很大。谢谢!
    • 很高兴我能帮上忙,别忘了接受答案;)
    猜你喜欢
    • 2019-12-25
    • 2022-11-30
    • 2021-12-18
    • 2021-09-02
    • 2020-05-26
    • 2018-09-19
    • 2019-12-11
    • 2020-09-22
    • 2023-01-19
    相关资源
    最近更新 更多