Package.json React SSR 生产应用程序的代理替代品
Package.json proxy alternative to React SSR production app
所以我想找到一种方法如何将 package.json 的这一部分实现到我的 server.js,它负责我的 React App 服务器端渲染。
"proxy": {
"/api/*": {
"target": "http://localhost:3501"
},
"/media/*": {
"target": "http://localhost:3501"
}
},
我查看了一些库,例如 express-http-proxy
和 http-proxy-middleware
,但找不到有效的解决方案。
我最后一次尝试:
import proxy from 'express-http-proxy';
...
app.use('/api', proxy('http://localhost:3501/api/*'));
app.use('/media', proxy('http://localhost:3501/media/*'));
它记录 404 并且路径基本正确,只是没有“/api/”应该是“/api/posts/”但只记录“/posts/”。
就这么简单。将您当前的代码替换为:
import proxy from 'http-proxy-middleware';
...
app.use('/media/*', proxy({target: 'http://localhost:3500', changeOrigin: true}));
所以我想找到一种方法如何将 package.json 的这一部分实现到我的 server.js,它负责我的 React App 服务器端渲染。
"proxy": {
"/api/*": {
"target": "http://localhost:3501"
},
"/media/*": {
"target": "http://localhost:3501"
}
},
我查看了一些库,例如 express-http-proxy
和 http-proxy-middleware
,但找不到有效的解决方案。
我最后一次尝试:
import proxy from 'express-http-proxy';
...
app.use('/api', proxy('http://localhost:3501/api/*'));
app.use('/media', proxy('http://localhost:3501/media/*'));
它记录 404 并且路径基本正确,只是没有“/api/”应该是“/api/posts/”但只记录“/posts/”。
就这么简单。将您当前的代码替换为:
import proxy from 'http-proxy-middleware';
...
app.use('/media/*', proxy({target: 'http://localhost:3500', changeOrigin: true}));