{"id":13671570,"url":"https://github.com/expressjs/vhost","last_synced_at":"2025-04-12T21:18:57.552Z","repository":{"id":14709930,"uuid":"17430308","full_name":"expressjs/vhost","owner":"expressjs","description":"virtual domain hosting","archived":false,"fork":false,"pushed_at":"2023-05-24T12:09:20.000Z","size":92,"stargazers_count":763,"open_issues_count":3,"forks_count":85,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-04-05T20:01:31.795Z","etag":null,"topics":["expressjs","javascript","middleware","nodejs","vhost"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"AngularClass/NG6-starter","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/expressjs.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2014-03-05T06:31:16.000Z","updated_at":"2025-03-13T11:47:54.000Z","dependencies_parsed_at":"2023-02-13T16:46:16.891Z","dependency_job_id":"d4e8921c-739f-4230-b520-fecd0a7e371f","html_url":"https://github.com/expressjs/vhost","commit_stats":{"total_commits":164,"total_committers":3,"mean_commits":"54.666666666666664","dds":"0.012195121951219523","last_synced_commit":"934d6ba704eaa0e42033d274044182ce5cb8bd76"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/expressjs%2Fvhost","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/expressjs%2Fvhost/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/expressjs%2Fvhost/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/expressjs%2Fvhost/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/expressjs","download_url":"https://codeload.github.com/expressjs/vhost/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248546186,"owners_count":21122272,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["expressjs","javascript","middleware","nodejs","vhost"],"created_at":"2024-08-02T09:01:13.554Z","updated_at":"2025-04-12T21:18:57.534Z","avatar_url":"https://github.com/expressjs.png","language":"JavaScript","readme":"# vhost\n\n[![NPM Version][npm-image]][npm-url]\n[![NPM Downloads][downloads-image]][downloads-url]\n[![Build Status][github-actions-ci-image]][github-actions-ci-url]\n[![Test Coverage][coveralls-image]][coveralls-url]\n\n## Install\n\n```sh\n$ npm install vhost\n```\n\n## API\n\n```js\nvar vhost = require('vhost')\n```\n\n### vhost(hostname, handle)\n\nCreate a new middleware function to hand off request to `handle` when the incoming\nhost for the request matches `hostname`. The function is called as\n`handle(req, res, next)`, like a standard middleware.\n\n`hostname` can be a string or a RegExp object. When `hostname` is a string it can\ncontain `*` to match 1 or more characters in that section of the hostname. When\n`hostname` is a RegExp, it will be forced to case-insensitive (since hostnames are)\nand will be forced to match based on the start and end of the hostname.\n\nWhen host is matched and the request is sent down to a vhost handler, the `req.vhost`\nproperty will be populated with an object. This object will have numeric properties\ncorresponding to each wildcard (or capture group if RegExp object provided) and the\n`hostname` that was matched.\n\n```js\nvar connect = require('connect')\nvar vhost = require('vhost')\nvar app = connect()\n\napp.use(vhost('*.*.example.com', function handle (req, res, next) {\n  // for match of \"foo.bar.example.com:8080\" against \"*.*.example.com\":\n  console.dir(req.vhost.host) // =\u003e 'foo.bar.example.com:8080'\n  console.dir(req.vhost.hostname) // =\u003e 'foo.bar.example.com'\n  console.dir(req.vhost.length) // =\u003e 2\n  console.dir(req.vhost[0]) // =\u003e 'foo'\n  console.dir(req.vhost[1]) // =\u003e 'bar'\n}))\n```\n\n## Examples\n\n### using with connect for static serving\n\n```js\nvar connect = require('connect')\nvar serveStatic = require('serve-static')\nvar vhost = require('vhost')\n\nvar mailapp = connect()\n\n// add middlewares to mailapp for mail.example.com\n\n// create app to serve static files on subdomain\nvar staticapp = connect()\nstaticapp.use(serveStatic('public'))\n\n// create main app\nvar app = connect()\n\n// add vhost routing to main app for mail\napp.use(vhost('mail.example.com', mailapp))\n\n// route static assets for \"assets-*\" subdomain to get\n// around max host connections limit on browsers\napp.use(vhost('assets-*.example.com', staticapp))\n\n// add middlewares and main usage to app\n\napp.listen(3000)\n```\n\n### using with connect for user subdomains\n\n```js\nvar connect = require('connect')\nvar serveStatic = require('serve-static')\nvar vhost = require('vhost')\n\nvar mainapp = connect()\n\n// add middlewares to mainapp for the main web site\n\n// create app that will server user content from public/{username}/\nvar userapp = connect()\n\nuserapp.use(function (req, res, next) {\n  var username = req.vhost[0] // username is the \"*\"\n\n  // pretend request was for /{username}/* for file serving\n  req.originalUrl = req.url\n  req.url = '/' + username + req.url\n\n  next()\n})\nuserapp.use(serveStatic('public'))\n\n// create main app\nvar app = connect()\n\n// add vhost routing for main app\napp.use(vhost('userpages.local', mainapp))\napp.use(vhost('www.userpages.local', mainapp))\n\n// listen on all subdomains for user pages\napp.use(vhost('*.userpages.local', userapp))\n\napp.listen(3000)\n```\n\n### using with any generic request handler\n\n```js\nvar connect = require('connect')\nvar http = require('http')\nvar vhost = require('vhost')\n\n// create main app\nvar app = connect()\n\napp.use(vhost('mail.example.com', function (req, res) {\n  // handle req + res belonging to mail.example.com\n  res.setHeader('Content-Type', 'text/plain')\n  res.end('hello from mail!')\n}))\n\n// an external api server in any framework\nvar httpServer = http.createServer(function (req, res) {\n  res.setHeader('Content-Type', 'text/plain')\n  res.end('hello from the api!')\n})\n\napp.use(vhost('api.example.com', function (req, res) {\n  // handle req + res belonging to api.example.com\n  // pass the request to a standard Node.js HTTP server\n  httpServer.emit('request', req, res)\n}))\n\napp.listen(3000)\n```\n\n## License\n\n[MIT](LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/vhost.svg\n[npm-url]: https://npmjs.org/package/vhost\n[coveralls-image]: https://img.shields.io/coveralls/expressjs/vhost/master.svg\n[coveralls-url]: https://coveralls.io/r/expressjs/vhost\n[downloads-image]: https://img.shields.io/npm/dm/vhost.svg\n[downloads-url]: https://npmjs.org/package/vhost\n[github-actions-ci-image]: https://img.shields.io/github/actions/workflow/status/expressjs/vhost/ci.yml?branch=master\u0026label=ci\n[github-actions-ci-url]: https://github.com/expressjs/vhost/actions/workflows/ci.yml\n","funding_links":[],"categories":["JavaScript","中间件","NodeJS"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexpressjs%2Fvhost","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexpressjs%2Fvhost","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexpressjs%2Fvhost/lists"}