[英]how can I properly proxy requests to /api/ressource in prod?
I have a webpack dev configuration with my front end dev server running on 8080 and my backed server running on port 3000. 我有一个webpack开发配置,前端开发服务器运行在8080上,后备服务器运行在端口3000上。
So in dev mode my webpack dev server is configured like follows : 所以在开发模式下,我的webpack开发服务器配置如下:
proxy: {
'/api': 'http://localhost:3000',
}
How can I do the same thing in the prod server that serves the built static files of my front end ? 如何在提供前端内置静态文件的prod服务器中执行相同操作?
I have the following code for my prod server that serves the static files of my front end : 我的产品服务器具有以下代码,该代码可提供前端的静态文件:
const proxy = require('http-proxy-middleware')
app.use(express.static(dir))
/**
* Redirect everything that starts with /api/* to the backend rest server
*/
app.use('/api', proxy({ target: backendUrl }))
app.get('*', (req, res) => {
res.sendFile(path.resolve(dir + '/index.html'))
})
This is not working as the cookies seem to be lost with the proxying (unlike with the proxying with webpack where evyrhthing works). 这不起作用,因为cookie似乎随代理一起丢失(与evyrhthing起作用的webpack代理不同)。
Am I going about this problem in the correct way ? 我要以正确的方式解决这个问题吗?
In this case, you can create a reverse-proxy
which is going to receive all the information from the frontend, make the requests to the other address and then return the proper answer to the frontend. 在这种情况下,您可以创建一个reverse-proxy
,它将接收来自前端的所有信息,向其他地址发出请求,然后将正确的答案返回给前端。 I used to develop a lot of these back in the days, there is a package that i created which can help you. 过去,我曾经开发过很多这样的程序 ,我创建了一个程序包可以为您提供帮助。
Basically the flow is: 基本上,流程是:
Frontend -> endpoint on your render server (port 8080) -> backend (port 3000) -> render server (port 8080) -> frontend
You can try using: 您可以尝试使用:
You may also want to check your cookie flags (changeOrigin, secure, cookieDomainRewrite etc..) 您可能还需要检查cookie标志(changeOrigin,secure,cookieDomainRewrite等。)
info: IF running http on localhost, the cookie will not be set if secure-flag is present in the response 信息:如果在本地主机上运行http,则如果响应中存在安全标志,则不会设置cookie
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.