简体   繁体   中英

Create/configure node.js dispatcher/proxy

I'm working on javascript Single Page Application with Aurelia framework and using simple fake backend(express.js) for prototyping purposes. Backend runs on localhost:8081 and client app on localhost:9000 There are some Cross Domain issues because these are different ports, and adding cross origin headers to the backend seems cumbersome to me.

What i want is simple dispatcher/proxy that would run on, say, localhost:3000 and redirect incoming calls in this manner (localhost:3000/app => localhost:9000) (localhost:3000/api => localhost:8081) Thus eliminating cross domain issues.

I'm looking for really simple solution, maybe there is some node.js app that suited just for such cases.

If you are using Express, you can add this routes to your app.

You need to install the module 'request' for this example

// At the top
var request = require('request');

And then:

//APP
app.use('/app', function (req, res) { request('http://localhost:9000' + req.originalUrl).pipe(res); });

//API
app.use('/api', function (req, res) { request('http://localhost:8081' + req.originalUrl).pipe(res); });

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