🌐 Nodejs.cn

虚拟主机中间件

安装

🌐 Install

Terminal window
npm install vhost

Note

vhost 不包含其自身的 TypeScript 类型定义。如果你使用 TypeScript,还需要作为开发依赖从 DefinitelyTyped 安装社区维护的类型:

Terminal window
npm install --save-dev @types/vhost

API

var vhost = require('vhost');

vhost(hostname, handle)

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

🌐 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 可以是一个字符串或一个 RegExp 对象。当 hostname 是字符串时,它可以包含 * 来匹配主机名部分中的一个或多个字符。当 hostname 是 RegExp 时,它将被强制为不区分大小写(因为主机名不区分大小写),并且将被强制根据主机名的起始和结束进行匹配。

当主机匹配并且请求被发送到虚拟主机处理程序时,req.vhost 属性将被填充为一个对象。该对象将具有与每个通配符(如果提供了 RegExp 对象,则为捕获组)对应的数字属性,以及匹配的 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.

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

使用 connect 进行静态服务

🌐 using with connect for static serving

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

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

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