node, express app returns text/javascript response

2019-09-05 23:22发布

问题:

I am trying to return jsonp from my express, nodejs application and I keep getting text/javascript instead of application/javascript (which I think should be the correct Content-Type). I am not bothered about IE < 8. The various ways that I have tried to set the content-type when returning the result is shown below:

// 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);

But no matter what, the Content-Type that I get is always text/javascript as shown below. I also get 'nosniff' header twice in the response headers :-/ I have it only once in my nginx.conf file which I have thoroughly checked. Even did a nginx -t and it says config is fine.

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

I am using nginx as a reverse proxy using the 'upstream thingy'. I am also using bodyParser in my application. Please let me know if any further information is needed as I am absolutely new to node/express. Thanks

回答1:

I'm afraid that the content type is hard coded in Express. See here.

By the looks of it, the only way around that is to re-implement .jsonp() yourself (or create an issue on GitHub for the Express devs to fix it).