最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJs app "hanging" or "freezing" whenever an error occurs - Stack Overflow

programmeradmin1浏览0评论

When I make multiple post requests to my nodejs server and all of the parameters are correct, everything works fine (and doesnt freeze) but when I make multiple post requests with incorrect parameters that gives an error, my nodejs server just freezes/hangs for a few minutes. Why is this?

Here is my code btw

app.post('/pushtransaction', function(req, res) {
    console.log(req.body);
    console.log(5);
    if (req.body.sigs) {
        let sigver = xmf.modules.ecc.Signature.from(req.body.sigs).toString();
        let lasig = [sigver];
        console.log(req.body.packedTr);
        let transi = JSON.parse(req.body.packedTr);

        //let sigver = req.body.sigs;
        let package = {
            pression: 'none',
            transaction: transi,
            signatures: lasig
        }

        console.log(package);
        //Pushes tx in correct format
        xmf.pushTransaction(package).then(result=>{
            res.send(result);
            res.end();
            console.log(result);
        }).catch(err => {
            console.log(err)
        });
    }
})

When I make multiple post requests to my nodejs server and all of the parameters are correct, everything works fine (and doesnt freeze) but when I make multiple post requests with incorrect parameters that gives an error, my nodejs server just freezes/hangs for a few minutes. Why is this?

Here is my code btw

app.post('/pushtransaction', function(req, res) {
    console.log(req.body);
    console.log(5);
    if (req.body.sigs) {
        let sigver = xmf.modules.ecc.Signature.from(req.body.sigs).toString();
        let lasig = [sigver];
        console.log(req.body.packedTr);
        let transi = JSON.parse(req.body.packedTr);

        //let sigver = req.body.sigs;
        let package = {
            pression: 'none',
            transaction: transi,
            signatures: lasig
        }

        console.log(package);
        //Pushes tx in correct format
        xmf.pushTransaction(package).then(result=>{
            res.send(result);
            res.end();
            console.log(result);
        }).catch(err => {
            console.log(err)
        });
    }
})
Share Improve this question asked May 25, 2018 at 22:56 user9760741user9760741 1
  • How many times are you making "multiple post requests". Javascript naturally processes errors slower than error free code, so that may be part of the problem. – YAHsaves Commented May 25, 2018 at 23:07
Add a ment  | 

3 Answers 3

Reset to default 4

When your error is encountered, your Node server does not know what to do other than console.log() the error. It needs to end that request and send some response. You can res.status(400).send({ error: err }) when you're within the catch.

Make sure res.send() method gets called every time in your request.

Updated Javascript:

app.post('/pushtransaction', function(req, res) {
    console.log(req.body);
    console.log(5);
    if (req.body.sigs) {
        let sigver = xmf.modules.ecc.Signature.from(req.body.sigs).toString();
        let lasig = [sigver];
        console.log(req.body.packedTr);
        let transi = JSON.parse(req.body.packedTr);

        //let sigver = req.body.sigs;
        let package = {
            pression: 'none',
            transaction: transi,
            signatures: lasig
        }

        console.log(package);
        //Pushes tx in correct format
        xmf.pushTransaction(package).then(result=>{
            res.send(result);
            res.end();
            console.log(result);
        }).catch(err => {
            console.log(err);
            res.status(400).send();
        });
    }

    res.status(400).send();
})

Additionally you don't have to call res.end() if you call res.send(). see Must res.end() be called in express with node.js?

Adding to other answers, you can add a middleware for timeouts, if any service fails to respond in some time, like

var timeout = require('connect-timeout');
app.use(timeout('5s'));
发布评论

评论列表(0)

  1. 暂无评论