Scenario: Consider the following code to give a JavaScript as a response from the Node.JS server.
var http = require('http');
http.createServer(function (req, res) {
var JS_Script = 'function Test() { alert("test success")}';
res.setHeader('content-type', 'text/javascript');
res.end(JS_Script);
}).listen(8811);
Issue: It forcing the browser to download the file.
Question: How can I make it to render on browser?
Note: Working in .net web service with same HTTP-header: 'content-type', 'text/javascript'
So, Its clear following statement does not hold good.
If you want a web browser to render the HTML, you should change this to:
Content-Type: text/html
Update: On inspecting the content-type during the download its application/octet-stream
But the same when I hit a request with curl, the following is the output:
C:\>curl -is http://localhost:8811/
HTTP/1.1 200 OK
Content-Type: text/javascript
Content-Length: 166
Date: Tue, 09 Apr 2013 10:31:32 GMT
Connection: keep-alive
function Test() { alert("test success")}
use application/javascript as content type instead of text/javascript.
text/javascript is mentioned obsolete. see reference docs.
http://www.iana.org/assignments/media-types/application
also see this question on SO.
UPDATE:
i have tried executing the code you have given and the below dint work.
res.setHeader('content-type', 'text/javascript');
res.send(JS_Script);
this is what worked for me.
res.setHeader('content-type', 'text/javascript');
res.end(JS_Script);
as robertklep has suggested, please refer to the node http docs, there is no response.send() there.