Proxy with express.js

前端 未结 9 1490
别那么骄傲
别那么骄傲 2020-11-28 00:29

To avoid same-domain AJAX issues, I want my node.js web server to forward all requests from URL /api/BLABLA to another server, for example other_domain.co

9条回答
  •  借酒劲吻你
    2020-11-28 01:22

    I found a shorter solution that does exactly what I want https://github.com/http-party/node-http-proxy

    After installing http-proxy

    npm install http-proxy --save
    

    Use it like below in your server/index/app.js

    var proxyServer = require('http-route-proxy');
    app.use('/api/BLABLA/', proxyServer.connect({
      to: 'other_domain.com:3000/BLABLA',
      https: true,
      route: ['/']
    }));
    

    I really have spent days looking everywhere to avoid this issue, tried plenty of solutions and none of them worked but this one.

    Hope it is going to help someone else too :)

提交回复
热议问题