Express服务器未使用正确的cors标头答复

时间:2018-09-23 03:50:37

标签: javascript node.js express nginx cors

我期望Allow-Access-Control标头出现在响应标头中,但我什么也没得到。我可以肯定这不是我的nginx配置,而是我的快速配置,但是我已经调整了cors标头并将其添加到所有可用的响应变量中。

service did not publish,error:["NSNetServicesErrorDomain": 10, "NSNetServicesErrorCode": -72004]

来自服务器的响应头

handleClick = () => {
        auth.getToken(this.refs.username.value, this.refs.password.value)
        .then(this.props.history.push('/list'))
        .catch(this.props.history.push('/'));
    }

nginx服务器配置

const path = require('path');
var cors = require('cors');
var express = require('express');
var app = express();
var walk = require('walk');
var ALLfiles = [];



app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
app.use("/puntington", express.static("/puntington"));

app.get('/puntington', function(req, res) {

  //EDIT: If you need to go under subdirs: change glob to walk as said in https://stackoverflow.com/questions/2727167/getting-all-filenames-in-a-directory-with-node-js/25580289#25580289
  var walker = walk.walk('./puntington', {
    followLinks: false
  });
  walker.on('file', function(root, stat, next) {
    // Add this file to the list of files
    ALLfiles.push(path.join("https://static.maxrobbins.com/images/listPuntyImages/puntington", stat.name));
    next();
  });

  walker.on('end', function() {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'DELETE, PUT, GET, POST');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.send(ALLfiles);
  });

});

var server = app.listen(8666, function() {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});

1 个答案:

答案 0 :(得分:0)

我知道了。因为我同时在nginx中设置了CORS标头,并表示浏览器感到困惑和大便,所以我的意思是我在响应标头中得到了重复的CORS设置。通过从nginx网站配置中删除CORS标头,仅使用快速设置,一切正常。我感谢帮助男孩<3