简体   繁体   English

Package.json替代React SSR生产应用程序的代理

[英]Package.json proxy alternative to React SSR production app

So I would like to find a way how to implement this part of package.json to my server.js which is responsible for my React App server side rendering. 所以我想找到一种方法来将package.json的这一部分实现到负责我的React App服务器端渲染的server.js中。

"proxy": {
    "/api/*": {
      "target": "http://localhost:3501"
    },
    "/media/*": {
      "target": "http://localhost:3501"
    }
  },

I have looked into some of the libraries like express-http-proxy and http-proxy-middleware but I cannot find a working solution. 我研究了一些库,例如express-http-proxyhttp-proxy-middleware但找不到有效的解决方案。

Last I tried: 最后我尝试了:

import proxy from 'express-http-proxy';
...

app.use('/api', proxy('http://localhost:3501/api/*'));

app.use('/media', proxy('http://localhost:3501/media/*'));

It logs 404 and the path is basically right, just without "/api/" should be "/api/posts/" but logs only "/posts/". 它记录404,并且路径基本上是正确的,只是没有“ / api /”应该是“ / api / posts /”,但是只记录“ / posts /”。

Use http-proxy-middleware 使用http-proxy-middleware

It is just as simple as that. 就这么简单。 Replace your current code with this: 将此替换为当前代码:

import proxy from 'http-proxy-middleware';

...

app.use('/media/*', proxy({target: 'http://localhost:3500', changeOrigin: true}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM