An email company is sending a POST request to my API, they want me to send them a 200 response asap before I do any additional processing.
Can I send the 200 from the server-usergrid.js and still be able to execute some additional code in another JS file? I’m new to node.js so I’m not sure if the send command stops additional coding.
app.post('/postEmailReceipt', function (req, res) { res.setHeader("Access-Control-Allow-Origin","*"); if (!req.is('json')) { res.status(400).send('Bad request not JSON'); return; }else{ res.sendStatus(200); ReceiptController.postEmailReceipt(req, res); } });
Solved! Go to Solution.
Dear @jerryhamby,
Execution doesn't stop even if you send status back to client in Node.JS program. Your code still executes after res.sendStatus...
See example below..
var express = require('express'); var app = express(); app.get('/', function(req, res){ res.sendStatus('200'); console.log("Code still executes..."); }); app.listen(3000);
Cheers,
Anil Sagar