Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js: route request to different port on same host

I have a host computer which serves a number of webapplications (not node.js based). It does this using different ports. This means that for example the following applications are live:

  • app1: http://hostname:3000
  • app2: http://hostname:3001
  • app3: http://hostname:3003

Next to that I have a node.js based webapp (running on port 80) which I want to use as a sort of router. When someone navigates to http://localhost/app/app1. I want it to navigate to http://hostname:3000. This is relatively straightforward using a simple redirect. However, I would want to preserve the url http://localhost/app/app1. Can someone point me to a way to make this work using node.js/express?

My routing logic looks somewhat like this (pseudo-code).

app.route('/app/:appName')
   .get(appEngine.gotoApp);

appEngine.gotoApp = function(req, res) {
    redirectToApp logic 
    }
like image 862
Bart Avatar asked May 13 '15 10:05

Bart


2 Answers

You probably better use Nginx setting up a reverse proxy with different locations per application.

It's not what you ask for because it does not use node.js, but if it's the only purpose, Nginx really suits your needs.

For example a Nginx configuration file like should work the way you want :

server {
    listen 80;

    server_name myapp.com;

    location /app1 {
        proxy_pass http://APP_PRIVATE_IP_ADDRESS:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    } 

    location /app2 {
        proxy_pass http://APP_PRIVATE_IP_ADDRESS:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    } 

    location /app3 {
        proxy_pass http://APP_PRIVATE_IP_ADDRESS:3003;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    } 
}
like image 99
Blackus Avatar answered Nov 15 '22 22:11

Blackus


If you use express, you can try to create the app with the cli express application generator.

It creates an express app and returns it with module exports.

In the server.js file it pass to listen function of the server instance the express app object.

You can create more server object and listen different app with different port.

var server = http.createServer(app);
server.listen(port);
var server2 = http.createServer(app2);
server2.listen(port2);

If you want to point different app based on the url, you can instance an express router instead of express object.

var app1 = express.Router();

Then you can set all your routes into this object with classic get or post or other methods.

Now you are able to pass the router as a middleware of your main express app.

app.use( "app1/", app1 );

You can also pass an express app to middleware, instead of router object, in order to gain the possibility of exec the app with a different url and port server listening.

like image 24
Luca Colonnello Avatar answered Nov 15 '22 21:11

Luca Colonnello