Amol M Kulkarni Amol M Kulkarni - 3 months ago 17
Node.js Question

Node.js res.setHeader('content-type', 'text/javascript'); pushing the response javascript as file download

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');

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
during the download its

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.

also see this question on SO.


i have tried executing the code you have given and the below dint work.

res.setHeader('content-type', 'text/javascript');

this is what worked for me.

res.setHeader('content-type', 'text/javascript');

as robertklep has suggested, please refer to the node http docs, there is no response.send() there.