vhost middleware
Installa
npm install vhostyarn add vhostpnpm add vhostbun add vhostvlt install vhostNote
vhost does not include its own TypeScript type definitions. If you use TypeScript, also install the community-maintained types from DefinitelyTyped as a development dependency:
npm install --save-dev @types/vhostyarn add --dev @types/vhostpnpm add --save-dev @types/vhostbun add --dev @types/vhostvlt install --save-dev @types/vhostAPI
var vhost = require('vhost');vhost(hostname, handle)
Crea una nuova funzione middleware per consegnare la richiesta di handle quando l’host
in arrivo per la richiesta corrisponde a hostname. La funzione è chiamata come
handle(req, res, next), come un middleware standard.
hostname può essere una stringa o un oggetto RegExp. Quando hostname è una stringa può
contenere * per corrispondere a 1 o più caratteri in quella sezione del nome host. Quando
hostname è un RegExp, sarà costretto a caso-insensibile (dal momento che i nomi host sonoformat@@1
e sarà costretto a corrispondere in base all’inizio e alla fine del nome host.
Quando l’host viene abbinato e la richiesta viene inviata a un gestore vhost, la proprietà req.vhost
sarà popolata con un oggetto. Questo oggetto avrà proprietà numeriche
corrispondenti a ogni jolly (o gruppo di acquisizione se l’oggetto RegExp è fornito) e il hostname
che è stato abbinato.
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' }));Esempi
uso con connessione per servizio statico
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 subdomainvar staticapp = connect();staticapp.use(serveStatic('public'));
// create main appvar app = connect();
// add vhost routing to main app for mailapp.use(vhost('mail.example.com', mailapp));
// route static assets for "assets-*" subdomain to get// around max host connections limit on browsersapp.use(vhost('assets-*.example.com', staticapp));
// add middlewares and main usage to app
app.listen(3000);utilizzando con la connessione per i sottodomini utente
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 appvar app = connect();
// add vhost routing for main appapp.use(vhost('userpages.local', mainapp));app.use(vhost('www.userpages.local', mainapp));
// listen on all subdomains for user pagesapp.use(vhost('*.userpages.local', userapp));
app.listen(3000);utilizzo con qualsiasi gestore generico di richieste
var connect = require('connect');var http = require('http');var vhost = require('vhost');
// create main appvar 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 frameworkvar 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);