简体   繁体   中英

Forward all requests in in Node.js/Koa server from http://server/api/* to another ip?

I'm using Node.js and Koa to build a web server. However, I'd like to merge it with another server located at 192.168.1.5:80. How can I make it so that all requests to {my web server}:80/api/* are exactly the same as requests to 192.168.1.5:80/api/*.

Thanks in advance!

You could use koa-proxy:

https://www.npmjs.com/package/koa-proxy

Then for the proxy, your code could look like this:

var koa = require('koa');
var proxy = require('koa-proxy');
var app = koa();
app.use(proxy({
    host:  'http://192.168.1.5', // proxy to 192.168.1.5:80... 
    match: /^\/api\//        // ...just the /api routes 
}));
app.listen(80);

hope that helps ... (not testet)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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