Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/someimportantcompany/koa-vhost
Virtual host splitting for Koa.js v2
https://github.com/someimportantcompany/koa-vhost
koa koa2 koajs vhosts
Last synced: 25 days ago
JSON representation
Virtual host splitting for Koa.js v2
- Host: GitHub
- URL: https://github.com/someimportantcompany/koa-vhost
- Owner: someimportantcompany
- License: mit
- Created: 2021-04-03T13:11:05.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T06:27:56.000Z (about 2 years ago)
- Last Synced: 2024-04-25T17:22:20.200Z (10 months ago)
- Topics: koa, koa2, koajs, vhosts
- Language: JavaScript
- Homepage:
- Size: 491 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![NPM](https://badge.fury.io/js/%40someimportantcompany%2Fkoa-vhost.svg)](https://npm.im/@someimportantcompany/koa-vhost)
[![CI](https://github.com/someimportantcompany/koa-vhost/actions/workflows/ci.yml/badge.svg)](https://github.com/someimportantcompany/koa-vhost/actions/workflows/ci.yml)
[![Coverage](https://coveralls.io/repos/github/someimportantcompany/koa-vhost/badge.svg)](https://coveralls.io/github/someimportantcompany/koa-vhost)Virtual host splitting for [Koa.js](https://koajs.com) v2.
## Install
```
$ npm install --save @someimportantcompany/koa-vhost
```## Usage
```js
const Koa = require('koa');
const vhost = require('@someimportantcompany/koa-vhost');const app = new Koa();
app.use(vhost('s1.example.com', ctx => {
ctx.status = 200;
ctx.body = 'server s1';
}));app.use(vhost(/(s2|s3).example.com/, ctx => {
ctx.status = 200;
ctx.body = 'server s2 or s3';
}));app.use(vhost([ 's4.example.com', 's5.example.com' ], ctx => {
ctx.status = 200;
ctx.body = 'server s4 or s5';
}));app.use(vhost('t2.example.com', ctx => {
ctx.status = 200;
ctx.body = 'server t2';
}));app.use(ctx => {
ctx.status = 200;
ctx.body = 'generic server';
});app.listen(3000);
```Then, assuming [httpie](https://httpie.io/):
```
$ http --print=HhBb http://localhost:54321
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:54321
User-Agent: HTTPie/1.0.3HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 14
Content-Type: text/plain; charset=utf-8
Date: Sat, 03 Apr 2021 12:47:33 GMT
Keep-Alive: timeout=5generic server
``````
$ http --print=HhBb http://localhost:54321 Host:s1.example.com
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: s1.example.com
User-Agent: HTTPie/1.0.3HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 9
Content-Type: text/plain; charset=utf-8
Date: Sat, 03 Apr 2021 12:48:43 GMT
Keep-Alive: timeout=5server s1
``````
$ http --print=HhBb http://localhost:54321 Host:s2.example.com
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: s1.example.com
User-Agent: HTTPie/1.0.3HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 9
Content-Type: text/plain; charset=utf-8
Date: Sat, 03 Apr 2021 12:48:43 GMT
Keep-Alive: timeout=5server s2 or s3
```## API
### `vhost(hosts, middleware)`
Option | Description
---- | ----
`hosts` | Either a `Function`, `Array`, `RegExp` or `String` of the hosts to match.
`middleware` | Middleware `Function` to execute if the hostname matches.## Notes
- Any questions or suggestions please [open an issue](https://github.com/someimportantcompany/koa-vhost/issues).