JavaScript MIME类型为application/octet-stream nodejs [英] JavaScript MIME type is application/octet-stream nodejs

查看:66
本文介绍了JavaScript MIME类型为application/octet-stream nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为节点js编写一个组合器模块.我正在使用express来获取如下文件:

I'm writing a combiner module for node js. I'm using express to get the files like the following:

app.get('/combine/js/?files=scripts/file1.js;scripts/file2.js', function(req, res){
    res.contentType('text/javascript');
    res.end(content); //the combined files content
});

现在,在加载页面时,我在chrome中收到以下错误:资源被解释为脚本,但以MIME类型application/octet-stream传输"

Now, when the page is loaded I'm getting the following error in chrome: 'Resource interpreted as Script but transferred with MIME type application/octet-stream'

我在做什么错了?

更新:这是响应内容的完整app.get组合器功能...

    app.get('/combiner/:type/?', function(req, res){
        var type = req.params.type;
        var files = [];
        files = req.query.files.split(';');
        var content = combiner.combine(type, files);
        switch(type){
                case 'js': res.contentType('text/javascript'); break;
            case 'css': res.contentType('text/css'); break;
        }
        content = content.replace('<:=appid=:>', vars.appid);
        res.end(content);
    });

推荐答案

据我所知,Express文档似乎在这里是错误的.他们清楚地说,给mime输入类型是可行的,但这对您本人或我来说都不行.

From what I can see, it looks like the Express docs might be wrong here. They clearly say that giving the mime type will work, but that isn't happening for you, or me in my tests.

您应该自动设置内容类型:

You should either set the content-type automatically:

res.header('Content-Type', 'application/javascript');

或将文件扩展名传递到 res.contentType().

or pass the file extension to res.contentType().

res.contentType('js');

这篇关于JavaScript MIME类型为application/octet-stream nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆