$ npm install vhost
var vhost = require('vhost')
Create a new middleware function to hand off request to 创建一个新的中间件函数,当请求的传入主机与handle
when the incoming host for the request matches hostname
. hostname
匹配时,将请求转交给handle
。The function is called as 以handle(req, res, next)
, like a standard middleware.handle(req, res, next)
的形式调用该函数,类似于标准中间件。
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. hostname
可以是字符串或RegExp对象。当hostname
是一个字符串时,它可以包含*
,以匹配主机名的该部分中的1个或多个字符。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.hostname
是RegExp时,它将被迫不区分大小写(因为主机名是不区分大小写的),并将被迫根据主机名的开始和结束进行匹配。
When host is matched and the request is sent down to a vhost handler, the 当主机匹配并且请求被发送到vhost处理程序时,req.vhost
property will be populated with an object. req.vhost
属性将填充一个对象。This object will have numeric properties corresponding to each wildcard (or capture group if RegExp object provided) and the 此对象将具有与每个通配符(如果提供RegExp对象,则为捕获组)和匹配的hostname
that was matched.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":对于"foo.bar.example.com:8080"与"*.*.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'
}))
var connect = require('connect')
var serveStatic = require('serve-static')
var vhost = require('vhost')
var mailapp = connect()
// add middlewares to mailapp for mail.example.com为mail.example.com向mailapp添加中间件
// 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将vhost路由添加到邮件的主应用程序
app.use(vhost('mail.example.com', mailapp))
// route static assets for "assets-*" subdomain to get路由"assets-*"子域的静态资产以获取
// around max host connections limit on browsers浏览器上的最大主机连接限制
app.use(vhost('assets-*.example.com', staticapp))
// add middlewares and main usage to app向应用程序添加中间件和主要用途
app.listen(3000)
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将中间件添加到主网站的mainapp
// create app that will server user content from public/{username}/创建将从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假装请求是针对文件服务的/{username}/*
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为主应用程序添加vhost路由
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任何框架中的外部api服务器
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处理属于api.example.com的req+res
// pass the request to a standard Node.js HTTP server将请求传递到标准Node.js HTTP服务器
httpServer.emit('request', req, res)
}))
app.listen(3000)