Categories
JavaScript Answers

How to host multiple Node.js sites on the same IP/server with different domains?

Spread the love

To host multiple Node.js sites on the same IP/server with different domains, we call the listen method with the domain.

For instance, we write

const server = require("diet");

const app = server();
app.listen("http://example.com/");
app.get("/", ($) => {
  $.end("hello world ");
});

const sub = server();
sub.listen("http://subdomain.example.com/");
sub.get("/", ($) => {
  $.end("hello world at sub domain!");
});

const other = server();
other.listen("http://other.com/");
other.get("/", ($) => {
  $.end("hello world at other domain");
});

to create the server with the diet package.

Then we call listen with the domain or subdomain URL to list to request to.

We call get with each server to listen for get request from /.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *