简体   繁体   English

节点express.js在发送标头后无法设置标头。”

[英]node express.js Can't set headers after they are sent.'

I am new to both node and express so I figure I am doing something stupid. 我对这两个节点都表示陌生,所以我认为自己在做一些愚蠢的事情。

Complete source code can be found at: 完整的源代码可以在以下位置找到:

https://github.com/wa1gon/aclogGate/tree/master/server https://github.com/wa1gon/aclogGate/tree/master/server

logRouter.get("/loggate/v1/listall", function(req, res) {
    let countStr = req.param('count');
    let count: number;

    if (!countStr) {
        count = null;
    } else {
        count = Number.parseInt(countStr);
        if (count == NaN) count = null;
    }

    acConn.listAllDatabase(count, (err: string, result: Array<LogGateResp>) => {
        console.log("got list all data resp")
         return res.json(result).end();
    });


    }

    );
    app.use('/', logRouter);

It works the first time though, but blows up the second. 虽然它第一次工作,但是第二次却炸毁了。

listallDatabase connects to a network socket which gets XML database back, parses it and calls back with an JS object. listallDatabase连接到一个网络套接字,该套接字将XML数据库取回来,对其进行解析并使用JS对象进行回调。 Which in turn calls res.json. 依次调用res.json。

Suggestions? 建议?

Remove the end() after res.json(). 在res.json()之后删除end()。

res.josn() send the response to frontend and end() try to send the response again. res.josn()将响应发送到frontend,end()尝试再次发送响应。 That why you are getting the error. 那就是为什么您得到错误。 Because node.js don't allow the API to send response twice. 因为node.js不允许API发送两次响应。 Either use res.end() or res.json(). 使用res.end()或res.json()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:发送标头后无法设置标头。 表达js - Error: Can't set headers after they are sent. express js Node.js和Express.js错误:发送标头后无法设置标头 - Node.js and Express.js Error : Can't set headers after they are sent Node.js错误:发送标头后无法设置标头。 - Node.js Error: Can't set headers after they are sent. 节点js中出现错误“错误:发送后无法设置标头。” - getting error in node js "Error: Can't set headers after they are sent." node.js应用程序:抛出新错误(“发送头后无法设置头。”) - node.js app: throw new Error('Can\'t set headers after they are sent.') 错误:发送后无法设置标头。 节点.js - Error: Can't set headers after they are sent. node.js Express / node.js发送后无法设置标头 - Express / node.js Can't set headers after they are sent node.js / express:发送标头后无法设置标头 - node.js/express: Can't set headers after they are sent Express.js 路由重定向错误:发送后无法设置标头 - Express.js Rout Redirect Error: Can't set headers after they are sent Node.JS/Passport-CI-OIDC - 错误:发送后无法设置标头。 在 ServerResponse.OutgoingMessage.setHeader - Node.JS/Passport-CI-OIDC - Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM