Node.js: request a route to a different port on the same host

I have a host machine that is serving multiple web applications (not node.js). It does this using different ports. This means that, for example, the following applications are available:

Alongside this, I have a node.js web app (running on port 80) that I want to use as a kind of router. When someone navigates to http: // localhost / app / app1 . I want it to go to http: // hostname: 3000 . It's pretty easy using a simple redirect. However, I would like to keep the url http: // localhost / app / app1 . Can anyone point me to a way to make this work using node.js / express?

My routing logic looks something like this (pseudocode).

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

appEngine.gotoApp = function(req, res) {
    redirectToApp logic 
    }

      

+3


source to share


2 answers


You are probably better off using Nginx to set up a reverse proxy with different locations for each application.

This is not what you are asking for because it does not use node.js, but if that is the only purpose, Nginx really suits your needs.



For example, the Nginx config file 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;
    } 
}

      

+2


source


If you are using express, you can try building your app using the cli express app generator.

It builds an express app and returns it when exporting modules.

In the server.js file, it goes to listen for the server instance as an express application object.

You can create more server objects and listen to different applications using a different port.

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

      

If you want to specify a different url based application, you can specify an express router instead of an express object.



var app1 = express.Router();

      

Then you can set all your routes to this object using the classic get or post method or other methods.

You can now pass the router as your main express application middleware.

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

      

You can also pass the express application to middleware rather than a router object to be able to run the application with a different server and port server.

+1


source







All Articles