黑人的命也是命。
支持平等正义倡议.

vhost

NPM Version NPM Downloads Build Status Test Coverage

安装

$ npm install vhost

API

var vhost = require('vhost')

vhost(hostname, handle)

创建一个新的中间件函数,当请求的传入主机与 hostname 匹配时,将请求传递给 handle。该函数以 handle(req, res, next) 的形式调用,就像标准中间件一样。

hostname 可以是字符串或 RegExp 对象。当 hostname 是字符串时,它可以包含 * 来匹配主机名该部分中的一个或多个字符。当 hostname 是 RegExp 时,它将被强制为不区分大小写(因为主机名是不区分大小写的),并且将被强制根据主机名的开头和结尾进行匹配。

当主机匹配并且请求被发送到 vhost 处理程序时,req.vhost 属性将被填充一个对象。此对象将具有对应于每个通配符(如果提供了 RegExp 对象,则为捕获组)和匹配的 hostname 的数字属性。

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'
}))

示例

与 connect 一起使用进行静态服务

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)

与 connect 一起使用进行用户子域

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)

与任何通用请求处理程序一起使用

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)

许可证

MIT