带有http和https以及CORS问题的Express服务器设置
我在express server.js中有这样的配置:带有http和https以及CORS问题的Express服务器设置,express,cors,Express,Cors,我在express server.js中有这样的配置: app.use(function (request, response, next) { response.header('Access-Control-Allow-Origin', '*'); response.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); response.header( 'Access-Cont
app.use(function (request, response, next) {
response.header('Access-Control-Allow-Origin', '*');
response.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
response.header(
'Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, X-Api-Key'
);
next();
})
app.options('*', function (request, response) {
response.send(200);
});
app.listen(httpPort, function () {
console.log('Listening on port: ' + httpPort);
});
httpsServer = https.createServer(credentials, app);
httpsServer.listen(httpsPort, function () {
console.log('Listening on port: ' + httpsPort);
});
此外,我必须指出:
及
目前,我正试图拨打至的电话,但收到CORS问题:
已阻止跨源请求:同一源策略不允许读取位于的远程资源。这可以通过将资源移动到同一域或启用CORS来解决
在Chrome上,我得到:
无法加载XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许“源”访问
如何对express server上的CORS问题进行排序此问题已排序:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://local:8001');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header(
'Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, X-Api-Key'
);
res.header('Access-Control-Allow-Credentials', 'true');
if ('OPTIONS' === req.method) {
res.sendStatus(200);
}
else {
next();
}
});