我想与Webpack devServer一起建立一个后端restful的cors代理。真正的模式可能如下:
https://localhost:3000/back-end/restful/apilocalhost:3000由Webpack devServer开放,https://back-end/restful/api是后端api.无论是get、post、put、delete,都可以用这种模式转发。有什么办法解决这个问题吗?
发布于 2020-05-11 12:22:24
上下文
你需要的是一个middleware。而且Express提供了一个用于使用CORS的工具。
示例
基于端点的实际使用示例如下:
为任何路线定义
const express = require('express');
const cors = require('cors');
const app = express();
const corsOptions = {
origin: 'http://example.com',
optionsSuccessStatus: 200
};
app.use(cors(corsOptions));
app.get('/back-end/restful/api', function (req, res, next) {
// Write your logic here
// ie:
res.json({ msg: 'CORS-enabled!' })
});为某些路线定义的
const express = require('express');
const cors = require('cors');
const app = express();
const corsOptions = {
origin: 'http://example.com',
optionsSuccessStatus: 200
};
app.get('/back-end/restful/api', cors(corsOptions), function (req, res, next) {
// Write your logic here
// ie:
res.json({ msg: 'CORS-enabled!' })
});在特快专递CORS项目上有进一步的文档。
https://stackoverflow.com/questions/61729467
复制相似问题