Proxy with express.js

前端 未结 9 1508
别那么骄傲
别那么骄傲 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:12

    request has been deprecated as of February 2020, I'll leave the answer below for historical reasons, but please consider moving to an alternative listed in this issue.

    Archive

    I did something similar but I used request instead:

    var request = require('request');
    app.get('/', function(req,res) {
      //modify the url in any way you want
      var newurl = 'http://google.com/';
      request(newurl).pipe(res);
    });
    

    I hope this helps, took me a while to realize that I could do this :)

提交回复
热议问题