简体   繁体   中英

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

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. Which in turn calls res.json.

Suggestions?

Remove the end() after res.json().

res.josn() send the response to frontend and end() try to send the response again. That why you are getting the error. Because node.js don't allow the API to send response twice. Either use res.end() or res.json().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM