Я ожидаю, что заголовки Allow-Access-Control будут присутствовать в заголовках ответа, но я ничего не получаю.Я вполне уверен, что это не моя конфигурация nginx, а скорее моя экспресс-конфигурация, однако я настроил и добавил заголовки cors ко всем доступным переменным ответа.
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 /1651513/kak-poluchit-spisok-imen-vseh-failov-prisutstvuyschih-v-kataloge-v-node-js#1651528
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);
});
Заголовки ответа от сервера
Connection
keep-alive
Content-Length
169
Content-Type
text/html
Date
Sun, 23 Sep 2018 03:29:29 GMT
Server
nginx/1.15.3
Конфигурация сервера nginx
location /puntington {
# auth_basic "Restricted Content";
# auth_basic_user_file /etc/nginx/sites-available/.htpasswd;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8666/puntington;
proxy_redirect off;
# auth_basic "Restricted Content";
# auth_basic_user_file /etc/nginx/sites-available/.htpasswd;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}