First of all, stop using writeHead everywhere. Because it completely rewrites the response headers.
If the tour is written as follows:
res.writeHead(200,{"coolHeader":"YesIAm"}); res.writeHead(500);
then node.js will only send a response with a status of 500 and without the "coolHeader" header;
If you want to change the status code, use
res.statusCode = ###;
If you want to add a new header use
res.setHeader("key", "value");
And if you want to rewrite all the headers, use writeHeader(...)
Secondly. Add this code
res.statusCode = 200; //... res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
instead
res.writeHead(200, { /// ... 'Access-Control-Allow-Origin' : '*' });
and replace all writeHead(###) with res.statusCode = ###;
source share