Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Express - separate routes on two ports

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?

like image 483
JKC Avatar asked Feb 27 '15 00:02

JKC


People also ask

How do I run node js on different ports?

If you want to run NodeJS on different port, change 80 to your required port number (e.g 8080). Also the function defined in createServer() will be executed whenever someone sends a request to your NodeJS server. In this case, it simply returns “Hello World!” string.

Can Express handle multiple requests?

Express. js use different kinds of middleware functions in order to complete the different requests made by the client for e.g. client can make get, put, post, and delete requests these requests can easily handle by these middleware functions.

What port should I use for Express?

js/Express. js App Only Works on Port 3000 - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


1 Answers

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

like image 136
JKC Avatar answered Sep 20 '22 15:09

JKC