manish
manish

Reputation: 13

use different routes for different ports in express app

Is it possible in express/node app to have different routes configured to different ports? Example: '/foo/bar' accessible to only localhost:3000 '/bar/foo' accessible to only localhost:3002

Upvotes: 1

Views: 4144

Answers (1)

jfriend00
jfriend00

Reputation: 707776

Yes, but you just create two servers, each on its own port and then create an express app object for each server and register routes for the desired server on the appropriate app object. A given server only listens on one port.

const express = require('express');

// first server
const app3000 = express();
app3000.get('/bar/foo', function(req, res) {
    // code here for port 3000 handler
});
app3000.listen(3000);

// second server
const app3002 = express();
app3002.get('/foo/bar', function(req, res) {
    // code here for port 3002 handler
});
app3002.listen(3002);

Upvotes: 4

Related Questions