vhost Documentation

Official TypeScript developer resources and documentation for expressjs/vhost. Verified technical context for AI.

0 stars TypeScript TypeScript 1 file ยท ~2,939 tokens
RAW Doc

1. Project Overview & Quickstart (expressjs/vhost)

vhost

NPM Version
NPM Downloads
Build Status
Test Coverage

Install

sh
$ npm install vhost

API

js
var vhost = require('vhost')

vhost(hostname, handle)

Create a new middleware function to hand off request to handle when the incoming
host for the request matches hostname. The function is called as
handle(req, res, next), like a standard middleware.

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.

js
var connect = require('connect')
var vhost = require('vhost')
var app = connect()

app.use(vhost('*.*.example.com', function handle (req, res, next) {
  // for match of "foo.bar.example.com:8080" against "*.*.example.com":
  console.dir(req.vhost.host) // => 'foo.bar.example.com:8080'
  console.dir(req.vhost.hostname) // => 'foo.bar.example.com'
  console.dir(req.vhost.length) // => 2
  console.dir(req.vhost[0]) // => 'foo'
  console.dir(req.vhost[1]) // => 'bar'
}))

Examples

using with connect for static serving

js
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)

using with connect for user subdomains

js
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.use(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)

using with any generic request handler

js
var connect = require('connect')
var http = require('http')
var vhost = require('vhost')

// create main app
var app = connect()

app.use(vhost('mail.example.com', function (req, res) {
  // handle req + res belonging to mail.example.com
  res.setHeader('Content-Type', 'text/plain')
  res.end('hello from mail!')
}))

// an external api server in any framework
var httpServer = http.createServer(function (req, res) {
  res.setHeader('Content-Type', 'text/plain')
  res.end('hello from the api!')
})

app.use(vhost('api.example.com', function (req, res) {
  // handle req + res belonging to api.example.com
  // pass the request to a standard Node.js HTTP server
  httpServer.emit('request', req, res)
}))

app.listen(3000)

License

MIT

2. Official Technical Reference & Guides (expressjs/expressjs.github.io)

[!CAUTION]
This repository is archived and no longer actively maintained.

We are no longer accepting issues, feature requests, or pull requests.
For additional support or questions, please visit the Express.js Discussions page.

The ExpressJS Organization

The ExpressJS Organization is a collection
of community-owned node.js web modules with shared maintenance.
Refactored from Connect, Express, and Koa,
these modules aim to be small, well-tested, focused, RFC-compliant,
and usable by any node.js framework or middleware.

Please let us know if you're interested in:

  • Maintaining some modules in the organization
  • Moving popular libraries into the organization

Background

Most of these libraries are based on many of TJ Holowaychuk's work.
Maintainers were slowly added to his projects to keep up with the maintenance burden.
But code maintenance isn't the sole issue;
maintaining the repositories themselves as well their maintainers is just as difficult.

GitHub organizations allow easy administration of multiple repositories,
their maintainers, as well as maintenance of their code.
With TJ Holowaychuk's retirement from node.js,
a robust framework to maintain his still widely-used modules is needed
by the community, and GitHub organizations are the answer.
With the ability to share npm publishing rights
very easily, no (still useful) module will be left behind.

With organizations comes a change in node's open source authorship philosophy.
npm places emphasis on the owner and author of a package.
Instead, we believe open source software should be community owned and maintained
as the original author is largely irrelevant if s/he no longer maintains the project.
With organizations, users won't have to worry about code rot,
ignored pull requests, or a single, overbearing czar.

Call for Maintainers

These modules need maintainers.
If you're interested in maintaining one of these modules,
please start contributing by making PRs and solving / discussing unsolved issues.

Team

These are the core team members, those that actively maintain more than one module.
You can also view all the public team members.

  • dougwilson - current lead maintainer of Express
    and Connect. Maintains much of Connect 2.x/Express 3.x's middleware
    as well as many tiny modules. A stickler for RFC-compliance, tests, and code-coverage.
  • jonathanong - maintains many of the core modules
    as well as Koa
  • fishrock123 - administrates and book-keeps
    many of the modules as well as maintains the mime-type-related libraries.
  • defunctzombie
  • mscdex

Honorary Members

Frameworks

These frameworks are actively maintained by members of the Organization.

Core Modules

Building a node.js web framework?
These are the modules you should be particularly interested in:

Our goal for open source software is to organize modules well as well as
share the maintenance burden so that no one has to worry about code rot.
The ExpressJS Organization is focused with node web modules,
but there are multiple organizations maintained in a similar manner.

If you're interested in joining or collaborating with these organizations,
please let us know!

Organizations for tiny utilities:

Other organizations you might be interested in:

  • detects - browser feature detects as tiny modules
  • node-modules - generic node modules
  • normalize - package management-less,
    build-less, and specification-compliant web development
  • polyfills - browser polyfills and
    user-agent-based polyfill combinations