visit
npm init
npm install express
npm install express-http-proxy
npm install request
You will see package.json is created in your project directory. Now create the server.js file and paste below code inside it.
const proxy = require('express-http-proxy');
const app = require('express')();
const express = require('express')
const request = require('request');
app.use('/api', proxy('//localhost:3000')); // this will proxy all incoming requests to /api route to back end
app.use('/client', proxy('//localhost:5000')); // this will proxy all incoming requests to /client route to front end
app.listen(4000, () => {
request('//localhost:5000', function (err, res, body) {
if(err === null){
console.log('backend is reachable from proxy server')
}
else{
console.log('backend is not reachable from proxy server')
}
});
request('//localhost:3000', function (err, res, body) {
if(err === null){
console.log('frontend is reachable from proxy server')
}
else{
console.log('frontend is not reachable from proxy server')
}
});
});
Now is the easiest part, just run below command to run proxy server and make sure your backend and frontend are live at that time.
node server.js // you can user forever or pm2 to run it in infinite loop
There you go if you are now serving two different apps from one port.
As always here is some programming humor for you
Stop it Patrick, you’re scaring him 😄 <a href="//medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">//medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a>