Ausnahme gefangen: SSL certificate problem: certificate is not yet valid ๐Ÿ“Œ How to Create and Manage Virtual Domains using Node.js

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š How to Create and Manage Virtual Domains using Node.js


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Imagine that you have two or more subdomains, and you want to serve two different web applications. However, you do not want to create a different web server application for each subdomain.

In this kind of situation, Node.js allows developers to manage virtual domains within a single web server application using vhost library.

In this post, we will learn how to create and manage virtual domains using Vhost.

What is Vhost ?

As already said, Vhost is a Node.js library that allows developers to manage virtual domains within a single web server application. It provides a configurable middleware function that accepts two arguments. The first one is the hostname. The second argument is the request handler which will be called when the hostname matches. The hostname follows the same rules as route paths do. They can be either a string or a regular expression.

Getting ready with Vhost

First, initialize the project by opening a terminal and running :

npm init

Then, install the necessary dependencies by running:

npm install vhost express

How to do itโ€ฆ

build two mini-applications using Router that will be served in two different sub-domains:

Create a new file named virtual-domains.js Include vhost NPM module. Then, initialize a new ExpressJS application:

import express from โ€˜expressโ€™
import vhost from โ€˜vhostโ€™
const app = express()

Define two routers that we will use to build two mini-applications:

const app1 = express.Router()
const app2 = express.Router()

Add a route method to handle GET requests for path โ€œ/โ€ in the first router:

app1.get('/', (req, res, next) => {
res.send('This is the main application.')
})

Add a route method to handle GET requests for path โ€œ/โ€ in the second router:

app2.get('/', (req, res, next) => {
res.send('This is a second application.')
})

Mount our routers to our ExpressJS application. Serve the first application under localhost and the second under second.localhost:

app.use(vhost('localhost', app1))
app.use(vhost('second.localhost', app2))

Listen on port 1337 for new connections:

app.listen(
1337,
() => console.log('Web Server running on port 1337'),
)

Save the file

Open a terminal and run:

node virtual-domains.js

To see the result, in your web browser navigate to:

http://localhost:1337/
http://second.localhost:1337/

That is it! We have created two domains name related to the same server.

Thereโ€™s moreโ€ฆ

Vhost adds a Vhost object to the request object, which contains the complete hostname (displaying the hostname and port), hostname (without the port), and matching strings. These give you more control over how to handle virtual domains. For example, we could write an application that allows users to have their own sub-domain with their name:

Create a new file named user-subdomains.js

Include the vhost NPM module. Then, initialize a new ExpressJS application:

Import express from โ€˜expressโ€™
Import vhost from โ€˜vhostโ€™
const app = express()

Define a new router. Then, add a route method to handle GET requests on the path โ€œ/โ€. Use the vhost object to access the array of subdomains:

const users = express.Router()
users.get('/', (req, res, next) =
> {
const username = req
.vhost[0]
.split('-')
.map(name => (
name[0].toUpperCase() +
name.slice(1)
))
.join(' ')
res.send(`Hello, ${username}`)
})

Mount the router:

app.use(vhost('*.localhost', users))

Listen on port 1337 for new connections:

app.listen(
1337,
() => console.log('Web Server running
on port 1337'),
)

Save the file

Open a terminal and run:

node user-subdomains.js

To see the result, in your web browser, navigate to:

http://john-thomas.localhost:1337/
http://jx-huang.localhost:1337/
http://superman.localhost:1337/

Conclusion

We did it! Now that you have known how to create and manage virtual domains, itโ€™s your chance to create something amazing with it. Maybe a video conference application that allows users to have their own sub-domain with their room name.

You can find the complete source code for this small application in this repository.

...



๐Ÿ“Œ How to Create and Manage Virtual Domains using Node.js


๐Ÿ“ˆ 52.56 Punkte

๐Ÿ“Œ How to Create and Manage Virtual Machines with the Vagrant Command Line Tool


๐Ÿ“ˆ 27.33 Punkte

๐Ÿ“Œ How to manage Node.js vulnerabilities [11 of 26] | Beginner's Series to Node.js


๐Ÿ“ˆ 27.31 Punkte

๐Ÿ“Œ How to create a web API with Node.js and Express [17 of 26] | Beginner's Series to Node.js


๐Ÿ“ˆ 25.93 Punkte

๐Ÿ“Œ .Science and .study: Domains of the bookish? More like domains of the JERKS!


๐Ÿ“ˆ 25.33 Punkte

๐Ÿ“Œ How to Create and Manage Users Using Useradd Linux Command


๐Ÿ“ˆ 24.99 Punkte

๐Ÿ“Œ How to Create and Manage Tasks Using Windows Task Scheduler


๐Ÿ“ˆ 24.99 Punkte

๐Ÿ“Œ Virtual Pets, Virtual People and Virtual Immortality


๐Ÿ“ˆ 24.15 Punkte

๐Ÿ“Œ How to create a new Node.js project [6 of 26] | Beginner's Series to Node.js


๐Ÿ“ˆ 24.14 Punkte

๐Ÿ“Œ .new: Google gibt Domains zum Erstellen neuer Inhalte zur Registrierung frei โ€“ das sind die ersten Domains


๐Ÿ“ˆ 23.54 Punkte

๐Ÿ“Œ Anzeige | Domains registrieren: So kommst du an deine Wunsch-Domains


๐Ÿ“ˆ 23.54 Punkte

๐Ÿ“Œ Anzeige | Domains registrieren: So kommst du an deine Wunsch-Domains


๐Ÿ“ˆ 23.54 Punkte

๐Ÿ“Œ Anzeige | Domains registrieren: So kommst du an deine Wunsch-Domains


๐Ÿ“ˆ 23.54 Punkte

๐Ÿ“Œ Full Stack Project Tutorial โ€“ Create a Recipe App Using React, Node.js and PostgreSQL


๐Ÿ“ˆ 22.7 Punkte

๐Ÿ“Œ How do Wix/Squarespace manage their SSL certs for custom domains?


๐Ÿ“ˆ 22.4 Punkte

๐Ÿ“Œ How to create the smartest multilingual Virtual Assistant using AWS and ChatGPT


๐Ÿ“ˆ 21.82 Punkte

๐Ÿ“Œ Using Node File Router for file-based routing in Node.js


๐Ÿ“ˆ 21.8 Punkte

๐Ÿ“Œ Medium CVE-2020-10505: The school manage system project The school manage system


๐Ÿ“ˆ 21.26 Punkte

๐Ÿ“Œ Medium CVE-2020-10507: The school manage system project The school manage system


๐Ÿ“ˆ 21.26 Punkte

๐Ÿ“Œ Medium CVE-2020-10506: The school manage system project The school manage system


๐Ÿ“ˆ 21.26 Punkte

๐Ÿ“Œ Create a CLI tool to help bootstraping Flutter project using Node.JS - Part 2


๐Ÿ“ˆ 20.92 Punkte

๐Ÿ“Œ How To Create An AI Chatbot with Google GEMINI using Node.js


๐Ÿ“ˆ 20.92 Punkte

๐Ÿ“Œ Create a Folder in Batch File: How to Create Directories Using Batch Scripts


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ Create Deployment Using โ€œkubectl create deploymentโ€


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ Microsoft Create: How to create a logo using PowerPoint


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ Microsoft Create: Create a month of social media posts using Designer


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ Microsoft Create: Create a Pinterest pin using Designer


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ Microsoft Create: How to create bulletin board letters or banners using PowerPoint


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ Microsoft Create: Create a UGC portfolio using PowerPoint


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ How to Create an Ubuntu Virtual Machine on Windows 10 Using Hyper-V


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ Create Virtual Environment in Python Using Conda


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ BEGINNER-FRIENDLY STEP BY STEP GUIDE ON HOW TO CREATE AWS VIRTUAL MACHINE USING WINDOWS 10


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ How to Create Virtual Machine in Proxmox VE Using Web UI


๐Ÿ“ˆ 20.03 Punkte

๐Ÿ“Œ How to Create and Manage Your Own Android Emulators | The Xamarin Show


๐Ÿ“ˆ 19.87 Punkte











matomo