https://github.com/chrisyip/koa-better-vhost
https://github.com/chrisyip/koa-better-vhost
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/chrisyip/koa-better-vhost
- Owner: chrisyip
- License: mit
- Created: 2015-11-22T09:26:45.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-11-22T15:28:20.000Z (over 10 years ago)
- Last Synced: 2025-05-14T22:12:14.238Z (about 1 year ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-better-vhost
[![Node version][node-image]][npm-url] [![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Travis CI][travis-image]][travis-url] [![Codecov][codecov-image]][codecov-url]
vhost for koajs.
Forked from [koa-vhost](https://github.com/Treri/koa-vhost).
# Install
```js
npm i koa-better-vhost
```
# Example
```js
const koa = require('koa')
const Router = require('koa-router')
const vhost = require('koa-better-vhost')
const main = koa()
main.use(require('koa-compress')())
main.use(function* (next) {
this.today = new Date
yield next
})
let vhosts = []
let homepage = koa()
let homepageRouter = new Router()
homepageRouter.get('/', function* (next) {
// `this` is shared between main and homepage applications
this.body = `Hello ${this.today.toUTCString()}`
yield next
})
homepage.use(homepageRouter.routes())
vhosts.push({
host: 'example.com',
app: homepage
})
let api = koa()
api.use(function* auth (next) {
// Do some auth job
yield next
})
let apiRouter = new Router()
apiRouter.get('/', function* (next) {
/**
* Because we set this vhost is isolated, `this` is NOT shared between main and api applications
* But `koa-compress` is still working
*/
console.info(typeof this.today) // undefined
this.body = `Hello from API`
yield next
})
api.use(apiRouter.routes())
vhosts.push({
host: /^api\.example\.com$/i,
app: api,
isolated: true
})
main.use(vhost(vhosts))
```
[node-image]: http://img.shields.io/node/v/koa-better-vhost.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-better-vhost
[npm-image]: http://img.shields.io/npm/v/koa-better-vhost.svg?style=flat-square
[daviddm-url]: https://david-dm.org/chrisyip/koa-better-vhost
[daviddm-image]: http://img.shields.io/david/chrisyip/koa-better-vhost.svg?style=flat-square
[travis-url]: https://travis-ci.org/chrisyip/koa-better-vhost
[travis-image]: http://img.shields.io/travis/chrisyip/koa-better-vhost.svg?style=flat-square
[codecov-url]: https://codecov.io/github/chrisyip/koa-better-vhost
[codecov-image]: https://img.shields.io/codecov/c/github/chrisyip/koa-better-vhost.svg?style=flat-square