Skip to content

evheniy/yeps-virtual-host

Repository files navigation

YEPS Virtual Host

It helps to work with different domains splitting http / https protocols

NPM

npm version Build Status Coverage Status Linux Build Windows Build

Dependency Status devDependency Status NSP Status

License GitHub stars GitHub forks GitHub issues Twitter

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());

About

yeps-virtual-host

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published