7

I need to simulate four servers (with different host and port) on the same nodejs instance.

An example could be:

domain1:8000 - domain2:8010 - domain3:8020 - domain4:8030 -

Can anybody please help me? Thanks

McKenzie
  • 103
  • 1
  • 1
  • 5
  • 1
    Create multiple servers (http or whatever), and make them listen to different ports... – Seblor Jul 31 '18 at 18:56
  • How can I create them? – McKenzie Jul 31 '18 at 19:10
  • There are some examples of HTTP servers in the official doc : https://nodejs.org/api/http.html **edit :** I suggest you take a look at the links in there : https://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js – Seblor Jul 31 '18 at 19:17
  • How can you separate the host by the way ? Port I understand . – rootExplorr Jul 31 '18 at 20:14

3 Answers3

12

I add an example with a possible solution for 2 servers using node.

First, you need to create a project:

mkdir simulate4servers
cd simulate4servers
npm init (entry point: index.js)

Install Express:

npm install express --save

Put the next files into the directory:

File app.js:

'use strict';
const express = require('express');
const app = express();
const routes = require('routes.js');

app.use('/',routes);

module.exports = app;

File app2.js:

'use strict';

const express = require('express');
const app2 = express();
const routes = require('routes2.js');

app2.use('/',routes);

module.exports = app2;

File configDomain1.js:

module.exports = {
    port: 8000
}

File configDomain2.js:

module.exports = {
    port: 8010
}

File routes.js:

'use strict';

const express = require('express');
const api = express.Router();

api.get('/', (req,res)=>{
  res.send({message: 'Hello World!'});
});
module.exports = api;

File routes2.js:

'use strict';

const express = require('express');
const api = express.Router();

api.get('/', (req,res)=>{
  res.send({message: 'Hello World 2!'});
});
module.exports = api;

File index.js:

'use strict';

const app = require('./app')
const app2 = require('./app2')
const config = require('./configDomain1');
const config2 = require('./configDomain2');

app.listen(config.port, () => {
    console.log(`API REST running in http://localhost:${config.port}`);
});

app2.listen(config2.port, () => {
    console.log(`API REST running in http://localhost:${config2.port}`);
});

And finally, run it:

node index.js
fernandezbcn
  • 151
  • 4
7
const express = require("express");

const server1 = express();
const server2 = express();
const server3 = express();

server1.listen(8000, () => {
    console.log("Server 1 is up and running on port 8000");
})

server2.listen(8010, () => {
    console.log("Server 2 is up and running on port 8010");
})

server3.listen(8020, () => {
    console.log("Server 3 is up and running on port 8020");
})

express() creates and returns an instance of a server. call it multiple times to create multiple instances. listen on different ports. that's all.

Aniket Kariya
  • 1,471
  • 2
  • 21
  • 26
2

This answer is correct https://stackoverflow.com/a/65159130/17576982. But it should also include server.use(cors()) else it may give error cors policy no 'access-control-allow-origin'.

Code to make multiple servers:

const express = require('express')
const cors = require('cors')
server=express()
server2=express()
server.use(cors())
server2.use(cors())

server.get('/',(req,res)=>res.send({"server":"3001","name":"aakash4dev","data":"data 1"}))
server2.get('/',(req,res)=>res.send({"server":"3002","name":"aakash4dev","data":"data 2"}))

server.listen(3001)
server2.listen(3002)
aakash4dev
  • 774
  • 4
  • 13