What is vhost?
The vhost npm package is used to create virtual hosts in a Node.js application. It allows you to define different hostnames for different parts of your application, enabling you to serve multiple domains from a single server.
What are vhost's main functionalities?
Basic Virtual Host Setup
This code demonstrates how to set up two virtual hosts using the vhost package. Requests to app1.example.com will be handled by app1, and requests to app2.example.com will be handled by app2.
const express = require('express');
const vhost = require('vhost');
const app = express();
const app1 = express();
app1.get('/', (req, res) => {
res.send('Hello from app1.example.com!');
});
const app2 = express();
app2.get('/', (req, res) => {
res.send('Hello from app2.example.com!');
});
app.use(vhost('app1.example.com', app1));
app.use(vhost('app2.example.com', app2));
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Wildcard Subdomains
This code demonstrates how to use wildcard subdomains with the vhost package. Any subdomain of example.com will be handled by subApp, and the subdomain name will be available in req.vhost.
const express = require('express');
const vhost = require('vhost');
const app = express();
const subApp = express();
subApp.get('/', (req, res) => {
res.send(`Hello from ${req.vhost[0]}.example.com!`);
});
app.use(vhost('*.example.com', subApp));
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Other packages similar to vhost
http-proxy
The http-proxy package is a full-featured HTTP proxy for Node.js. It can be used to create a reverse proxy server, which can route requests to different backend servers based on the hostname or other criteria. Unlike vhost, which is specifically designed for virtual hosting within a single application, http-proxy is more general-purpose and can be used for a wider range of proxying tasks.
express-subdomain
The express-subdomain package allows you to define subdomain-specific routes in an Express application. It is similar to vhost in that it helps manage different parts of an application based on the hostname, but it is specifically focused on subdomains rather than full virtual hosts.
vhost
Install
$ npm install vhost
API
var vhost = require('vhost')
vhost(hostname, server)
Create a new middleware function to hand off request to server
when the incoming
host for the request matches hostname
.
hostname
can be a string or a RegExp object. When hostname
is a string it can
contain *
to match 1 or more characters in that section of the hostname. When
hostname
is a RegExp, it will be forced to case-insensitive (since hostnames are)
and will be forced to match based on the start and end of the hostname.
When host is matched and the request is sent down to a vhost handler, the req.vhost
property will be populated with an object. This object will have numeric properties
corresponding to each wildcard (or capture group if RegExp object provided) and the
hostname
that was matched.
req.vhost.host === 'foo.bar.example.com:8080'
req.vhost.hostname === 'foo.bar.example.com'
req.vhost.length === 2
req.vhost[0] === 'foo'
req.vhost[1] === 'bar'
Examples
using with connect for static serving
var connect = require('connect')
var serveStatic = require('serve-static')
var vhost = require('vhost')
var mailapp = connect()
var staticapp = connect()
staticapp.use(serveStatic('public'))
var app = connect()
app.use(vhost('mail.example.com', mailapp))
app.use(vhost('assets-*.example.com', staticapp))
app.listen(3000)
using with connect for user subdomains
var connect = require('connect')
var serveStatic = require('serve-static')
var vhost = require('vhost')
var mainapp = connect()
var userapp = connect()
userapp.use(function(req, res, next){
var username = req.vhost[0]
req.originalUrl = req.url
req.url = '/' + username + req.url
next()
})
userapp.use(serveStatic('public'))
var app = connect()
app.use(vhost('userpages.local', mainapp))
app.use(vhost('www.userpages.local', mainapp))
app.use(vhost('*.userpages.local', userapp))
app.listen(3000)
License
MIT