Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
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.
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');
});
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.
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.
Previously connect.vhost()
.
$ npm install vhost
var vhost = require('vhost')
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.
// for match of "foo.bar.example.com:8080" against "*.*.example.com":
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'
var connect = require('connect')
var serveStatic = require('serve-static')
var vhost = require('vhost')
var mailapp = connect()
// add middlewares to mailapp for mail.example.com
// create app to serve static files on subdomain
var staticapp = connect()
staticapp.use(serveStatic('public'))
// create main app
var app = connect()
// add vhost routing to main app for mail
app.use(vhost('mail.example.com', mailapp))
// route static assets for "assets-*" subdomain to get
// around max host connections limit on browsers
app.use(vhost('assets-*.example.com', staticapp))
// add middlewares and main usage to app
app.listen(3000)
var connect = require('connect')
var serveStatic = require('serve-static')
var vhost = require('vhost')
var mainapp = connect()
// add middlewares to mainapp for the main web site
// create app that will server user content from public/{username}/
var userapp = connect()
userapp.user(function(req, res, next){
var username = req.vhost[0] // username is the "*"
// pretend request was for /{username}/* for file serving
req.originalUrl = req.url
req.url = '/' + username + req.url
next()
})
userapp.use(serveStatic('public'))
// create main app
var app = connect()
// add vhost routing for main app
app.use(vhost('userpages.local', mainapp))
app.use(vhost('www.userpages.local', mainapp))
// listen on all subdomains for user pages
app.use(vhost('*.userpages.local', userapp)
app.listen(3000)
The MIT License (MIT)
Copyright (c) 2014 Jonathan Ong me@jongleberry.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
virtual domain hosting
The npm package vhost receives a total of 188,833 weekly downloads. As such, vhost popularity was classified as popular.
We found that vhost demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.