yeps-virtual-host
v1.0.1
Published
YEPS Virtual Host
Downloads
6
Maintainers
Readme
YEPS Virtual Host
It helps to work with different domains splitting http / https protocols
How to install
npm i -S yeps-virtual-host
How to use
YEPS config:
const App = require('yeps');
const VirtualHost = require('yeps-virtual-host');
const error = require('yeps-error');
const app = new App();
const vhost = new VirtualHost();
app.all([
error();
]);
HTTP:
vhost
.http('yeps.info')
.then(async ctx => {
ctx.res.statusCode = 200;
ctx.res.end('homepage');
});
HTTPS:
vhost
.https('yeps.info')
.then(async ctx => {
ctx.res.statusCode = 302;
ctx.res.setHeader('Location', 'http://yeps.info/');
ctx.res.end();
});
Any host:
vhost
.any('*.yeps.info')
.then(async ctx => {
ctx.res.statusCode = 302;
ctx.res.setHeader('Location', 'http://yeps.info/');
ctx.res.end();
});
All methods are wrappers for catch() method:
catch({
domain,
protocol: '*|http|https'
})
Example:
vhost
.catch({ domain: '*.yeps.info', protocol: '*' })
.then(async ctx => {
ctx.res.statusCode = 302;
ctx.res.setHeader('Location', 'http://yeps.info/');
ctx.res.end();
});
Add virtual host to app:
app.then(vhost.resolve());
With router:
const Router = require('yeps-router');
const siteRouter = new Router();
siteRouter.get('/').then(async ctx => {
ctx.res.statusCode = 200;
ctx.res.end('homepage');
});
vhost
.http('yeps.info')
.then(siteRouter.resolve());
const apiRouter = new Router();
apiRouter.get('/').then(async ctx => {
ctx.res.statusCode = 200;
ctx.res.setHeader('Content-Type', 'application/json');
ctx.res.end('{"status":"OK"}');
});
vhost
.http('api.yeps.info')
.then(apiRouter.resolve());
app.then(vhost.resolve());