简体   繁体   English

如何在prod中正确地将请求代理到/ api / ressource?

[英]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: 您可以尝试使用:

  • A server (iE nginx) as a reverse proxy. 服务器(iE nginx)作为反向代理。
  • A node-http-proxy as a reverse proxy. node-http-proxy作为反向代理。
  • A vhost middleware if each domain is served from the same Express codebase and node.js instance. 虚拟主机中间件(如果每个域都从相同的Express代码库和node.js实例提供服务)。

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.

相关问题 如何限制API请求的堆栈? - How can I throttle stack of api requests? 如何解决在 postman 和我的浏览器中正常工作的 API 请求,但返回 TypeError: Failed to fetch when used with fetch()? - How can I resolve API requests that work properly in postman and in my browser, but return TypeError: Failed to fetch when used with fetch()? 如何防止机器人和垃圾邮件 API 请求? - How can I prevent bots and spam API requests? 如何正确迭代 API 密钥数组以同步发出请求? - How do I properly iterate through an array of API keys to make requests, synchronously? 我如何以编程方式查看网站发出的请求(例如 API 或资源请求) - How can I programmatically see what requests (e.g. API or resource requests) are being made by a website 如何正确实现nodejs Stream API? - How can I properly implement the nodejs Stream API? 如何仅在 PROD 环境中隐藏元素? - How can I hide an element only in PROD environments? 如何从 SvelteKit 应用程序发送安全的 API 请求,而不在客户端显示 API 密钥? - How can I send secure API requests from SvelteKit app, without showing API keys on the client side? Google Places JavaScript API v3,如何将API密钥添加到请求中? - Google Places JavaScript API v3, how can I add my API key to requests? 如何在ExtJS代理中正确处理成功响应? - How do I properly handle a success response in an ExtJS proxy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM