节点,express应用程序返回text/javascript响应头(Content-Type),尽管设置了applic

node, express app returns text/javascript response header (Content-Type) in spite of setting application/javascript

本文关键字:Content-Type 设置 applic 响应 返回 应用程序 express text 节点 javascript      更新时间:2023-09-26

我正试图从我的express nodejs应用程序返回jsonp,我一直得到text/javascript而不是application/javascript(我认为这应该是正确的内容类型)。我不为IE<8.我在返回结果时尝试设置内容类型的各种方法如下所示:

// Method 1
res.setHeader('Content-Type', 'application/javascript');
res.status(200).jsonp(result);
// Method 2
res.format({
    'application/javascript': function() {
        res.status(200).jsonp(result);
    }
});
// Method 3
res.set('Content-Type', 'application/javascript');
res.status(200).jsonp(result);

但不管怎样,我得到的内容类型总是text/javascript,如下所示。我在响应头中也得到了两次"nosniff"头://我在nginx.conf文件中只得到了一次,我已经彻底检查过了。甚至做了一个nginx-t,它说配置很好。

HTTP/1.1 200 OK
Server: nginx
Date: Thu, 12 May 2016 05:06:28 GMT
Content-Type: text/javascript; charset=utf-8
Content-Length: 433
Connection: keep-alive
Keep-Alive: timeout=5
X-Powered-By: Express
Vary: Accept
X-Content-Type-Options: nosniff
ETag: W/"1b1-1ZnUnapTaayP/+6QW4iqXQ"
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000
Strict-Transport-Security: max-age=315360000; includeSubdomains
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Robots-Tag: none

我使用nginx作为反向代理,使用"upstreamthingy"。我还在我的应用程序中使用bodyParser。请让我知道是否需要任何进一步的信息,因为我是节点/快递的新手。感谢

恐怕内容类型在Express中是硬编码的。请参见此处。

从表面上看,唯一的解决办法是自己重新实现.jsonp()(或者在GitHub上创建一个问题,让Express开发人员来解决它)。