Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a MIME type before sending a file in Node.js?

Tags:

When sending scripts from my Node.js server to the browser, in Google Chrome, I get this warning:

Resource interpreted as Script but transferred with MIME type text/plain

I Google'd around, and found out that it's a server-side problem, namely, I think that I should set the correct MIME type to things, before sending them. Here's the HTTP server's handler:

var handler = function(req, res) {     url = convertURL(req.url); //I implemented "virtual directories", ignore this.      if (okURL(url)) //If it isn't forbidden (e.g. forbidden/passwd.txt)     {         fs.readFile (url, function(err, data)         {             if (err)             {                 res.writeHead(404);                 return res.end("File not found.");             }              //I think that I need something here.             res.writeHead(200);             res.end(data);         });     }     else //The user is requesting an out-of-bounds file.     {         res.writeHead(403);         return res.end("Forbidden.");     } } 

Question: How do I correct my server-side code to configure the MIME type correctly?

(Note: I already found https://github.com/broofa/node-mime, but it only lets me determine the MIME type, not to "set" it.)

like image 864
corazza Avatar asked Aug 15 '12 15:08

corazza


People also ask

How do I assign a MIME type?

In the Connections pane, go to the site, application, or directory for which you want to add a MIME type. In the Home pane, double-click MIME Types. In the MIME Types pane, click Add... in the Actions pane. In the Add MIME Type dialog box, add the file name extension and MIME type, and then click OK.

What is MIME in node js?

Mime (npm node-mimejs) is a capturing mock library for Node. js. It uses harmony-reflect Proxy objects (part of the ES 6 JavaScript standard) to allow for very simple capturing mock objects and capturing callbacks to be created and used within an automated test framework such as Mocha.


2 Answers

I figured it out!

Thanks to @rdrey's link and this node module I managed to correctly set the MIME type of the response, like this:

function handler(req, res) {     var url = convertURL(req.url);      if (okURL(url)) {         fs.readFile(url, function(err, data) {             if (err) {                 res.writeHead(404);                 return res.end("File not found.");             }              res.setHeader("Content-Type", mime.lookup(url)); //Solution!             res.writeHead(200);             res.end(data);         });     } else {         res.writeHead(403);         return res.end("Forbidden.");     } } 
like image 154
corazza Avatar answered Sep 24 '22 05:09

corazza


Search google for the Content-Type HTTP header.

Then figure out how to set it with http://expressjs.com/api.html#res.set

Oops, the example includes your answer ;)

Simply check the file ending, if it's .js, set the appropriate MIME type to make browsers happy.

EDIT: In case this is pure node, without express, look here: http://nodejs.org/api/http.html#http_response_setheader_name_value

like image 36
rdrey Avatar answered Sep 21 '22 05:09

rdrey