How do I set a MIME type before sending a file in

2020-02-05 01:27发布

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.)

4条回答
劳资没心,怎么记你
2楼-- · 2020-02-05 02:07

I had problems using your handler function because convertURL and okURL functions where not defined. I modified the code a little and finished looking like this

function handler(req, res) 
{
    // /home/juan/Documentos/push-api-demo is the path of the root directory of the server
    var url             = '/home/juan/Documentos/push-api-demo' + req.url;
    var file_exists     = fs.existsSync(url);

    if (file_exists) 
    {
        fs.readFile(url, function(err, data) 
        {
            if (err) 
            {
                res.writeHead(404);
                return res.end("File not found.");
            }

            res.setHeader("Content-Type", mime.lookup(url)); 
            res.writeHead(200);
            res.end(data);
        });
    } 
    else 
    {
        res.writeHead(403);
        return res.end("Forbidden.");
    }
}
查看更多
Juvenile、少年°
3楼-- · 2020-02-05 02:12

mime.lookup() is now renamed to mime.getType(). So you can do like this:

res.set('Content-Type', mime.getType('path/file'));

https://www.npmjs.com/package/mime

查看更多
小情绪 Triste *
4楼-- · 2020-02-05 02:19

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

查看更多
虎瘦雄心在
5楼-- · 2020-02-05 02:24

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.");
    }
}
查看更多
登录 后发表回答