Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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")} 
like image 657
Amol M Kulkarni Avatar asked Apr 09 '13 10:04

Amol M Kulkarni


People also ask

What is RES setHeader in node JS?

setHeader(name, value) (Added in v0. 4.0) method is an inbuilt application programming interface of the 'http' module which sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced.

How do you fix error Err_http_headers_sent ]: Cannot set headers after they are sent to the client?

To Solve Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Here You Need To use return statement to solve this error. Error Is occurs Because Of Your res. status(400). json is executing and then your function is not going to stop.

Can't render headers after they are sent to the client?

The "Cannot set headers after they are sent to the client" error occurs when the server in an express. js application sends more than one response for a single request, e.g. calling res. json() twice. To solve the error, make sure to only send a single response for each request.

Can not set headers after they are sent to client node JS?

The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written.


1 Answers

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 didn't 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.

like image 149
Mithun Satheesh Avatar answered Sep 20 '22 16:09

Mithun Satheesh