NodeJS Express - separate routes on two ports

后端 未结 2 1096
孤街浪徒
孤街浪徒 2020-12-23 21:12

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 ther

相关标签:
2条回答
  • 2020-12-23 21:53

    If you are trying to create multiple servers then why not crate multiple bin/www files with different ports and configurations. Another way could be pass port number directly from command line.

    0 讨论(0)
  • 2020-12-23 22:03

    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! :)

    0 讨论(0)
提交回复
热议问题