0

Assist me on how to achieve nodejs particulary expressjs framework. I want to have a website that can be structured with urls belonging to site as below:

  • store.example.com
  • account.example.com
  • learn.example.com
  • services.example.com

I can not figure out how to configure such mechanism in express.js

  • 3
    Does this answer your question? [How can I configure multiple sub domains in Express.js or Connect.js](https://stackoverflow.com/questions/5791260/how-can-i-configure-multiple-sub-domains-in-express-js-or-connect-js) – Phil Apr 11 '23 at 04:37
  • If it's all in 1 express application these are often called vhosts. – Evert Apr 11 '23 at 04:41

1 Answers1

0

First, from your command line, run:

npm install vhost

Then, in your express code:

const express = require("express");
const app = express();
const vhost = require("vhost");

app.use(vhost("store.example.com", function(req, res, next) {
  /*
  handle requests to store.example.com here
  */
}

app.use(vhost("account.example.com", function(req, res, next) {
  /*
  handle requests to account.example.com here
  */
}

Btw, such urls are called subdomains, the part before the "extension" (.com, .net etc.) is called second level domain (SLD) and the last part is called top level domain (TLD).

nare214
  • 337
  • 2
  • 7