How to Create virtual Sub-domain using Node Js - Coders Hut

How to Create virtual Sub-domain using Node Js

| |

How to Create virtual Sub-domain using Node Js

hello friends I am back with another awesome article  How to Create virtual Sub-domain using Node Jsso let’s start today’s article

Node Js is so popular these days for scalable applications and it is faster due to its async processing  so let’s see how to create a sub-domain using Node Js

so create a new folder and fire the command “npm  init -f”  and “npm install vhost express –save”

now create a file app.js file in root directory of your project

then import vhost and express modules in app.js

You May Also Like: Moded Apps APKHERE

and add above code in app.js file and now let’s understand what actually I coded

so first  import two modules vhost  & express 

You May Also Like: Certified Ethical Hacking Pdfs Free Download

then we create two separate servers using express()

  1. app
  2. main

then I  maintained manipulation of the request according to the subdomains for that manipulation i made use of vhost module

so our actual server is app which is listening on port 3000 so if anyone request on root directory without any subdomain prefix then app.get(“/”) will handle the request and give the response to client

if request made on any subdomain then first it will try to similar subdomain prefix is available on the server or not using app.use(“app.*”,function handle(req,res,next){})  example given in above code

similarly, if the request is made on then this case request handled by app.use(vhost(“main.*”, main)) which redirect it to main server

and if request made on then this case is going to handled by app.use(“pro.*”)

so that’s how you can create subdomains in node js and manipulate them without setting up any host file on the actual server

basically above method is not creating subdomain but all these subdomains which are coded in the above code act as VHOST and you can perform any manipulation you want

subscribe us for more node js tutorials and join us on telegram, thank you for reading share this article with your friends see you in the next article


How To install Redis DB in windows [UPDATED]

1 thought on “How to Create virtual Sub-domain using Node Js”

Leave a Comment