如何在发送Node.js文件之前设置MIME类型?

时间:2023-01-22 12:59:33

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

当从我的节点发送脚本时。js服务器到浏览器,在谷歌Chrome中,我得到以下警告:

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

将资源解释为脚本,但使用MIME类型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:

我四处谷歌,发现这是一个服务器端问题,也就是说,我认为应该在发送之前将正确的MIME类型设置为things。下面是HTTP服务器的处理程序:

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?

问题:如何纠正服务器端代码以正确配置MIME类型?

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

(注意:我已经找到了https://github.com/broofa/node-mime,但它只允许我确定MIME类型,而不是“设置”它。)

4 个解决方案

#1


15  

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:

感谢@rdrey的链接和这个节点模块,我成功地设置了响应的MIME类型,如下所示:

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.");
    }
}

#2


6  

Search google for the Content-Type HTTP header.

为内容类型的HTTP头搜索谷歌。

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

然后计算如何使用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.

只需检查文件的结尾,如果是.js,设置适当的MIME类型,让浏览器满意。

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

编辑:如果这是纯节点,没有表示,请查看这里:http://nodejs.org/api/http.html#http_response_setheader_name_value

#3


1  

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

我在使用处理器函数时遇到了问题,因为convertURL和okURL函数没有定义。我稍微修改了一下代码,就这样完成了。

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.");
    }
}

#4


1  

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

查阅()现在重命名为mime.getType()。你可以这样做:

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

#1


15  

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:

感谢@rdrey的链接和这个节点模块,我成功地设置了响应的MIME类型,如下所示:

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.");
    }
}

#2


6  

Search google for the Content-Type HTTP header.

为内容类型的HTTP头搜索谷歌。

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

然后计算如何使用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.

只需检查文件的结尾,如果是.js,设置适当的MIME类型,让浏览器满意。

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

编辑:如果这是纯节点,没有表示,请查看这里:http://nodejs.org/api/http.html#http_response_setheader_name_value

#3


1  

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

我在使用处理器函数时遇到了问题,因为convertURL和okURL函数没有定义。我稍微修改了一下代码,就这样完成了。

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.");
    }
}

#4


1  

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

查阅()现在重命名为mime.getType()。你可以这样做:

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