All Projects → expressjs → Vhost

expressjs / Vhost

Licence: mit
virtual domain hosting

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Vhost

deepword
Web editor based on Monaco
Stars: ✭ 25 (-96.36%)
Mutual labels:  middleware, expressjs
Session
Simple session middleware for Express
Stars: ✭ 5,571 (+712.1%)
Mutual labels:  middleware, expressjs
Frontexpress
An Express.js-Style router for the front-end
Stars: ✭ 263 (-61.66%)
Mutual labels:  middleware, expressjs
Compression
Node.js compression middleware
Stars: ✭ 2,506 (+265.31%)
Mutual labels:  middleware, expressjs
Body Parser
Node.js body parsing middleware
Stars: ✭ 4,962 (+623.32%)
Mutual labels:  middleware, expressjs
node-uploadx
Node.js middleware for handling resumable uploads
Stars: ✭ 17 (-97.52%)
Mutual labels:  middleware, expressjs
Method Override
Override HTTP verbs.
Stars: ✭ 548 (-20.12%)
Mutual labels:  middleware, expressjs
Express Recaptcha
Implementation of google recaptcha v2 & V3 solutions for express.js
Stars: ✭ 104 (-84.84%)
Mutual labels:  middleware, expressjs
Iris
The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | 谢谢 https://github.com/kataras/iris/issues/1329 |
Stars: ✭ 21,587 (+3046.79%)
Mutual labels:  middleware, expressjs
Express Openapi Validator
🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification
Stars: ✭ 436 (-36.44%)
Mutual labels:  middleware, expressjs
Csurf
CSRF token middleware
Stars: ✭ 2,183 (+218.22%)
Mutual labels:  middleware, expressjs
Serve Favicon
favicon serving middleware
Stars: ✭ 586 (-14.58%)
Mutual labels:  middleware, expressjs
Pure Http
✨ The simple web framework for Node.js with zero dependencies.
Stars: ✭ 139 (-79.74%)
Mutual labels:  middleware, expressjs
Cors
Node.js CORS middleware
Stars: ✭ 5,252 (+665.6%)
Mutual labels:  middleware, expressjs
Cookie Parser
Parse HTTP request cookies
Stars: ✭ 1,683 (+145.34%)
Mutual labels:  middleware, expressjs
Express Status Monitor
🚀 Realtime Monitoring solution for Node.js/Express.js apps, inspired by status.github.com, sponsored by https://dynobase.dev
Stars: ✭ 3,302 (+381.34%)
Mutual labels:  middleware, expressjs
Cookie Session
Simple cookie-based session middleware
Stars: ✭ 928 (+35.28%)
Mutual labels:  middleware, expressjs
Serve Static
Serve static files
Stars: ✭ 1,217 (+77.41%)
Mutual labels:  middleware, expressjs
Serve Index
Serve directory listings
Stars: ✭ 360 (-47.52%)
Mutual labels:  middleware, expressjs
Swagger Express Middleware
Swagger 2.0 middlware and mocks for Express.js
Stars: ✭ 543 (-20.85%)
Mutual labels:  middleware, expressjs

vhost

NPM Version NPM Downloads Build Status Test Coverage

Install

$ npm install vhost

API

var vhost = require('vhost')

vhost(hostname, handle)

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 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. 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.

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

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].