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.send(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")}
This question is related to
node.js
You can directly set the content type like below:
res.writeHead(200, {'Content-Type': 'text/plain'});
For reference go through the nodejs Docs link.
Source: Stackoverflow.com