How can I send a success status to browser from nodejs/express?

前端 未结 4 1391
南笙
南笙 2021-02-02 05:13

I\'ve written the following piece of code in my nodeJS/Expressjs server:

app.post(\'/settings\', function(req, res){
    var myData = {
        a: req.param(\'a\         


        
相关标签:
4条回答
  • 2021-02-02 05:36

    In express 4 you should do:

    res.status(200).json({status:"ok"})
    

    instead of the deprecated:

    res.json(200,{status:"ok"})
    
    0 讨论(0)
  • 2021-02-02 05:44

    Express Update 2015:

    Use this instead:

    res.sendStatus(200)
    

    This has been deprecated:

    res.send(200)  
    
    0 讨论(0)
  • 2021-02-02 05:45

    Jup, you need to send an answer back, the simplest would be

    res.send(200);
    

    Inside the callback handler of writeFile.

    The 200 is a HTTP status code, so you could even vary that in case of failure:

    if (err) {
        res.send(500);
    } else {
        res.send(200);
    }
    
    0 讨论(0)
  • 2021-02-02 05:46

    Just wanted to add, that you can send json via the res.json() helper.

    res.json({ok:true}); // status 200 is default
    

    res.json(500, {error:"internal server error"}); // status 500
    

    Update 2015:

    res.json(status, obj) has been deprecated in favor of res.status(status).json(obj)

    res.status(500).json({error: "Internal server error"});
    
    0 讨论(0)
提交回复
热议问题