Reprinted from: https://www.cnblogs.com/xiaoliwang/p/10195923.html
The development process often required to do the forwarding layer nodeJs
Use express with express-http-proxy can easily complete forward
Use process:
Installation of express-http-proxy
npm install express --save npm install express-http-proxy --save
Create a proxy instance
var proxy = require('express-http-proxy');
The client requests convey to http: // localhost: 3001:
app.use ( '/ API', Proxy ( 'HTTP: // localhost: 3001', { // filter (optional) filter: function (REQ, RES) { return req.method == 'the GET'; } // path resolution request (optional) proxyReqPathResolver: function (REQ) { the console.log ( `request path: $ {req.url}`); // request path: / Article This article was / List return `$ {REQ. ? url} token = 123456` // request forwarding path:? / Article This article was / token = 123456 List }, // returns the data processing, if the asynchronous operation procedure should return Promise (optional) userResDecorator: function (proxyRes, proxyResData, UserReq , UserRes) { // synchronization Data = the JSON.parse (proxyResData.toString ( 'UTF8')); data.newProperty = 'in my field adequately Data'; return the JSON.stringify (Data); //asynchronous return new Promise(function(resolve) { proxyResData.funkyMessage = 'oi io oo ii'; setTimeout(function() { resolve(proxyResData); }, 200); }); }, }))
If you have multiple servers, you can take advantage of express-http-proxy load balancing
app.use ( '/ API', Proxy (selectProxyHost)) // server randomly selected forwarding function selectProxyHost () { return (new new a Date ()% 2) 'http://google.com':? 'HTTP: // Altavista.com '; }