-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
关于跨域问题 #6
Comments
试试在server.js中设置: const { createProxyMiddleware } = require("http-proxy-middleware");
app.use(
"/api",
createProxyMiddleware({
target: "http://localhost:9527/api/admin",
changeOrigin: true,
ws: false,
pathRewrite: {
"^/api": "/"
}
})
); 我稍后把proxy的配置更新到代码里 |
使用最新版本的http-proxy-middleware 换个名字吧,我代码里面mock占用了 app.use(
"/proxy",
createProxyMiddleware({
target: "https://example.com", // 目标域名
changeOrigin: true,
ws: false,
pathRewrite: {
"^/proxy": "/",
},
})
); |
我试了一下,可以成功代理 下面这几行是mock配置,我的mock占用了 const mock = require("./mock/app-data"); // mock模拟数据,模拟后台业务
/** 监听POST请求,返回MOCK模拟数据 **/
app.post(/\/api.*/, (req, res, next) => {
const result = mock.mockApi({ url: req.originalUrl, body: req.body });
res.send(result);
});
app.get(/\/api.*/, (req, res, next) => {
const result = mock.mockApi({ url: req.originalUrl, body: req.body });
res.send(result);
}); |
找到问题所在了,算是一个小坑。你在server.js中用了body-parser,而我的node服务用了koa-body,两个同时存在的时候,会导致post请求挂起。我的解决方法是在server.js屏蔽如下两段代码:
或者createProxyMiddleware增加onProxyReq属性:
|
厉害了我的哥 nice |
向大佬学习~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello,感谢开源~ 请问如果要连接本地其他端口node后台服务需要怎样设置跨域? 我尝试在webpack.dev.config.js中加上如下代码,貌似不行
The text was updated successfully, but these errors were encountered: