npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

fastify-vhost

v1.3.6

Published

Proxy subdomain http requests to another server

Readme

fastify-vhost

npm-version coverage status known vulnerabilities downloads license

Proxy subdomain http requests to another server. This fastify plugin forwards all the requests received with a given subdomain to an upstream.

fastify-vhost is powered by the popular Nodejitsu http-proxy. GitHub stars

This plugin can be used if you want to point multiple (sub)domains to the same IP address, while running different servers on the same machine.

Fastify support

Prior to [email protected] we only supported [email protected]. We are proud to announce that fastify-vhost now supports both v1, v2 and v3!

Install

npm i fastify-vhost fastify

Example

const Fastify = require('fastify')
const server = Fastify()

server.register(require('fastify-vhost'), {
  upstream: 'http://localhost:3000',
  host: 'test.example.com'
})

server.listen(80)

This will proxy any request to the test subdomain to the server running at http://localhost:3000. For instance http://test.example.com/users will be proxied to http://localhost:3000/users.

If you want to have different vhosts for different subdomains you can register multiple instances of the plugin as shown in the following snippet:

const Fastify = require('fastify')
const server = Fastify()
const vhost = require('fastify-vhost')

server.register(vhost, {
  upstream: 'http://localhost:3000',
  host: 'test.example.com'
})

server.register(vhost, {
  upstream: 'http://localhost:3001',
  host: 'other.example.com'
})

server.listen(80)

You can also specify multiple aliases for each vhost with the hosts option:

const Fastify = require('fastify')
const server = Fastify()
const vhost = require('fastify-vhost')

server.register(vhost, {
  upstream: 'http://localhost:3000',
  hosts: ['test.example.com', 'test2.example.com']
})

server.register(vhost, {
  upstream: 'http://localhost:3001',
  host: 'other.example.com'
})

server.listen(80)

The above example would behave the same as the following:

const Fastify = require('fastify')
const server = Fastify()
const vhost = require('fastify-vhost')

server.register(vhost, {
  upstream: 'http://localhost:3000',
  host: 'test.example.com'
})

server.register(vhost, {
  upstream: 'http://localhost:3000',
  host: 'test2.example.com'
})

server.register(vhost, {
  upstream: 'http://localhost:3001',
  host: 'other.example.com'
})

server.listen(80)

But in a much neater way.

Notice that it is CRITICAL to provide the full host (subdomain + domain) so that it properly routes the requests across different upstreams.

For other examples, see example.js.

Options

This fastify plugin supports the following options.

Note that this plugin is fully encapsulated and payloads will be streamed directly to the destination.

upstream

An URL (including protocol) that the requests will be forwarded to (eg. http://localhost:3000).

host

The host to mount this plugin on. All the requests to the current server where the host header matches this string will be proxied to the provided upstream.

hosts

Equivalent to the host option, but an array of strings. All the requests to the current server where the host header matches any of the strings will be proxied to the provided upstream.

strict

Default: false. When strict mode is enabled, the host header has to be an exact match. When disabled, 'EXAMPLE.COM', 'example.com' and 'example.com:3000' will match 'example.com'.

timeout

Default: 30000. Timeout in milliseconds for the proxy to return a 504 Gateway Timeout.

Benchmarks

None yet. But you're welcome to open a PR.

TODO

  • [x] Add unit tests
  • [x] Add integration tests
  • [x] Coverage 100%
  • [ ] Add benchmarks

License

MIT