https://github.com/expressjs/cors을 사용하지 않는 이유는 무엇입니까? 이런 식으로 사용할 수 있습니다. 이 (내 이전 코멘트 당) 문제 밝혀졌다 때문에
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
app.listen(80, function() {
console.log('CORS-enabled web server listening on port 80')
})
또는 답변 여기에이 게시 https://enable-cors.org/server_expressjs.html
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.get('/', function(req, res, next) {
// Handle the get for this route
});
app.post('/', function(req, res, next) {
// Handle the post for this route
});
클라이언트에서 보내는 정확한 CORS 요청은 무엇입니까? 고객이 프리 플라잉을 결정한 경우 OPTIONS 요청을 지원해야 할 수도 있습니다. – jfriend00
@ jfriend00 네가 맞다. 이 url에 app.options를 추가했는데 이제 작동합니다. –