NodeJS Express - separate routes on two ports

27,910

Based on Explosion Pills suggestion above, I modified the code in roughly this way:

var express = require('express');
var things = [];
var app = express();
var admin_app = express();
var port = 8080; 
var admin_port = 8081;

app.post('/factory/', function(req, res) {
  //Create a thing and add it to the thing array
});

//Assume more functions to do to things here....

admin_app.post('/killallthings/', function(req, res) {
  //Destroy all the things in the array
});

admin_app.post('/listallthings/', function(req, res) {
  // Return a list of all the things
});

admin_app.post('/killserver/', function(req,res){
  //Kills the server after killing the things and doing clean up
});

//Assume https options properly setup.

var server = require('https').createServer(options, app);

server.listen(port, function() {
    logger.writeLog('Listening on port ' + port);
});

var admin_server = require('https').createServer(options, admin_app);

admin_server.listen(admin_port, function() {
    logger.writeLog('Listening on admin port ' + admin_port);
});

I wish I knew how to give Explosion Pills the credit for the answer! :)

Share:
27,910
JKC
Author by

JKC

Updated on August 18, 2020

Comments

  • JKC
    JKC over 3 years

    I have an express server, and while building it created several "helper" functions on their own routes. I'd like those routes to be accessed on a different port. Is there anyway to do this in express?

    In the code below, the "/factory" route (and other functionality) would be on one port, and the helper routes of "/killallthings", "/listallthings", and "/killserver" would be on a separate port.

    Here is a simplified version of the code:

    var express = require('express');
    var things = [];
    var app = express();
    var port = 8080; 
    
    app.post('/factory/', function(req, res) {
      //Create a thing and add it to the thing array
    });
    
    //Assume more functions to do to things here....
    
    app.post('/killallthings/', function(req, res) {
      //Destroy all the things in the array
    });
    
    app.post('/listallthings/', function(req, res) {
      // Return a list of all the things
    });
    
    app.post('/killserver/', function(req,res){
      //Kills the server after killing the things and doing clean up
    });
    
    //Assume https options properly setup.
    
    var server = require('https').createServer(options, app);
    
    server.listen(port, function() {
        logger.writeLog('Listening on port ' + port);
    });
    

    Is this possible with express?

  • user2348688
    user2348688 almost 8 years
    This may be a dumb question but any drawbacks to this config?? I ask because it seems so easy and I'm copying this right now. :)
  • JKC
    JKC almost 7 years
    a late response :) I'd guess the biggest drawback is security. for our app, it was an internal use only temporary testing tool, and the admin port was just to keep non-technical team members away from some functionality - no real security needed. in general, I wouldn't want to expose admin functionality with the port being the only protection. but it was a nice solution for what we needed.
  • YvesLeBorg
    YvesLeBorg over 5 years
    @JKC a late late response :) i like and am cc'ing this in my interface engine. Out-of-band admin we require, and there are many tools/tricks to render the admin access robust. THANKS a million.
  • Cemal
    Cemal about 5 years
    there are various reasons why it can be placed in the same file. Sharing resources may be one of them.
  • T Tse
    T Tse over 3 years
    The different port can has different firewall settings. I don't think there's much concern other than the two sets of code / credentials being in the same machine, and the two servers sharing one thread (if one goes down it might bring the other down, depending on implementation). The two servers in the same env might actually an an upside here though, seeing how "killServer" can be implemented quite easily since we have a reference to the server object.