1111$ npm install vhost
1212```
1313
14+ This package is ESM-only and requires Node.js ** 24 or newer** . TypeScript type
15+ declarations are bundled.
16+
1417## API
1518
1619``` js
17- var vhost = require ( ' vhost' )
20+ import vhost from ' vhost'
1821```
1922
2023### vhost(hostname, handle)
@@ -34,9 +37,9 @@ corresponding to each wildcard (or capture group if RegExp object provided) and
3437` hostname ` that was matched.
3538
3639``` js
37- var connect = require ( ' connect' )
38- var vhost = require ( ' vhost' )
39- var app = connect ()
40+ import connect from ' connect'
41+ import vhost from ' vhost'
42+ const app = connect ()
4043
4144app .use (vhost (' *.*.example.com' , function handle (req , res , next ) {
4245 // for match of "foo.bar.example.com:8080" against "*.*.example.com":
@@ -48,25 +51,35 @@ app.use(vhost('*.*.example.com', function handle (req, res, next) {
4851}))
4952```
5053
54+ ### TypeScript
55+
56+ The package ships type declarations. The shape assigned to ` req.vhost ` is exported as
57+ ` VHost ` , and ` vhost() ` is generic over the request and response types so it works with
58+ raw ` http ` , connect, or Express handlers without casting:
59+
60+ ``` ts
61+ import vhost , { type VHost } from ' vhost'
62+ ```
63+
5164## Examples
5265
5366### using with connect for static serving
5467
5568``` js
56- var connect = require ( ' connect' )
57- var serveStatic = require ( ' serve-static' )
58- var vhost = require ( ' vhost' )
69+ import connect from ' connect'
70+ import serveStatic from ' serve-static'
71+ import vhost from ' vhost'
5972
60- var mailapp = connect ()
73+ const mailapp = connect ()
6174
6275// add middlewares to mailapp for mail.example.com
6376
6477// create app to serve static files on subdomain
65- var staticapp = connect ()
78+ const staticapp = connect ()
6679staticapp .use (serveStatic (' public' ))
6780
6881// create main app
69- var app = connect ()
82+ const app = connect ()
7083
7184// add vhost routing to main app for mail
7285app .use (vhost (' mail.example.com' , mailapp))
@@ -83,19 +96,19 @@ app.listen(3000)
8396### using with connect for user subdomains
8497
8598``` js
86- var connect = require ( ' connect' )
87- var serveStatic = require ( ' serve-static' )
88- var vhost = require ( ' vhost' )
99+ import connect from ' connect'
100+ import serveStatic from ' serve-static'
101+ import vhost from ' vhost'
89102
90- var mainapp = connect ()
103+ const mainapp = connect ()
91104
92105// add middlewares to mainapp for the main web site
93106
94107// create app that will server user content from public/{username}/
95- var userapp = connect ()
108+ const userapp = connect ()
96109
97110userapp .use (function (req , res , next ) {
98- var username = req .vhost [0 ] // username is the "*"
111+ const username = req .vhost [0 ] // username is the "*"
99112
100113 // pretend request was for /{username}/* for file serving
101114 req .originalUrl = req .url
@@ -106,7 +119,7 @@ userapp.use(function (req, res, next) {
106119userapp .use (serveStatic (' public' ))
107120
108121// create main app
109- var app = connect ()
122+ const app = connect ()
110123
111124// add vhost routing for main app
112125app .use (vhost (' userpages.local' , mainapp))
@@ -121,12 +134,12 @@ app.listen(3000)
121134### using with any generic request handler
122135
123136``` js
124- var connect = require ( ' connect' )
125- var http = require ( ' http' )
126- var vhost = require ( ' vhost' )
137+ import connect from ' connect'
138+ import http from ' node: http'
139+ import vhost from ' vhost'
127140
128141// create main app
129- var app = connect ()
142+ const app = connect ()
130143
131144app .use (vhost (' mail.example.com' , function (req , res ) {
132145 // handle req + res belonging to mail.example.com
@@ -135,7 +148,7 @@ app.use(vhost('mail.example.com', function (req, res) {
135148}))
136149
137150// an external api server in any framework
138- var httpServer = http .createServer (function (req , res ) {
151+ const httpServer = http .createServer (function (req , res ) {
139152 res .setHeader (' Content-Type' , ' text/plain' )
140153 res .end (' hello from the api!' )
141154})
0 commit comments