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


I've written the following piece of code in my nodeJS/Expressjs server:'/settings', function(req, res){
    var myData = {
        a: req.param('a')
        ,b: req.param('b')
        ,c: req.param('c')
        ,d: req.param('d')

    var outputFilename = 'config.json';

    fs.writeFile(outputFilename, JSON.stringify(myData, null, 4), function(err) {
        if(err) {
        } else {
          console.log("Config file as been overwriten");

This allows me to get the submitted form data and write it to a JSON file.

This works perfectly. But the client remains in some kind of posting state and eventually times out. So I need to send some kind of success state or success header back to the client.

How should I do this?

Thank you in advance!

6/5/2019 1:47:34 PM

Accepted Answer

Express Update 2015:

Use this instead:


This has been deprecated:

3/29/2016 3:54:06 PM

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"});

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow