All Projects → keenwon → koa-subdomain

keenwon / koa-subdomain

Licence: MIT license
Simple and lightweight Koa middleware to handle multilevel and wildcard subdomains

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to koa-subdomain

koa-ip-filter
koa middleware to filter request IPs or custom ID with glob patterns, array, string, regexp or matcher function. Support custom 403 Forbidden message and custom ID.
Stars: ✭ 23 (+0%)
Mutual labels:  koa, koa-middleware
Koa2 Note
《Koa2进阶学习笔记》已完结🎄🎄🎄
Stars: ✭ 4,725 (+20443.48%)
Mutual labels:  koa, koajs
Koahub
KoaHub.js -- 中文最佳实践Node.js Web快速开发框架。支持Koa.js, Express.js中间件。当前项目已停止维护,推荐使用Doodoo.js
Stars: ✭ 308 (+1239.13%)
Mutual labels:  koa, koajs
restria
Entria's REST API boilerplate
Stars: ✭ 25 (+8.7%)
Mutual labels:  koa, koajs
Koahub Demo
koahub+async/await+mysql
Stars: ✭ 15 (-34.78%)
Mutual labels:  koa, koajs
koa-vue-view
A Koa view engine which renders Vue components on server.
Stars: ✭ 29 (+26.09%)
Mutual labels:  koa, koa-middleware
Koa Template
Starter template for Nuxt.js with KoaJS.
Stars: ✭ 374 (+1526.09%)
Mutual labels:  koa, koajs
Egg
🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
Stars: ✭ 17,616 (+76491.3%)
Mutual labels:  koa, koa-middleware
Koa Typeorm Starter
Starter project for using koa with TS and TypeORM
Stars: ✭ 23 (+0%)
Mutual labels:  koa, koajs
Vue Chat
👥Vue全家桶+Socket.io+Express/Koa2打造一个智能聊天室。
Stars: ✭ 887 (+3756.52%)
Mutual labels:  koa, koajs
koa-server
🗄️ GraphQL Back-end Server with Relay, Koa, MongoDB and Mongoose
Stars: ✭ 31 (+34.78%)
Mutual labels:  koa, koajs
Koach Javascript
Production ready Koa2 boilerplate.
Stars: ✭ 79 (+243.48%)
Mutual labels:  koa, koajs
koa
A golang framework like koa.js and has the best performance with net/http.
Stars: ✭ 30 (+30.43%)
Mutual labels:  koa, koajs
polix
🚀 Node.js Web Framework
Stars: ✭ 32 (+39.13%)
Mutual labels:  koa, koajs
koahub-cli
KoaHub CLI -- KoaHub.js的开发工具,自动babel编译 ES6/7(Generator Function, Class, Async & Await)并且文件修改后自动重启。
Stars: ✭ 16 (-30.43%)
Mutual labels:  koa, koajs
Koajs Design Note
《Koa.js 设计模式-学习笔记》已完结 😆
Stars: ✭ 520 (+2160.87%)
Mutual labels:  koa, koajs
Doodoo.js
Doodoo.js -- 中文最佳实践Node.js Web快速开发框架,支持Koa.js, Express.js中间件。
Stars: ✭ 57 (+147.83%)
Mutual labels:  koa, koajs
Cool Admin Api
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口
Stars: ✭ 188 (+717.39%)
Mutual labels:  koa, koajs
Koa Proxy
Proxy middleware for koa
Stars: ✭ 221 (+860.87%)
Mutual labels:  koa
koa-webpack-server
Koa2 webpack all-in-one environment for universal development
Stars: ✭ 14 (-39.13%)
Mutual labels:  koa-middleware

koa-subdomain

NPM version Node version Build Status Coverage Status npm download Dependencies Status Devdependencies Status

Simple and lightweight Koa middleware to handle multilevel and wildcard subdomains.

Installation

Install using npm:

npm install koa-subdomain --save

xhr-plus

Usage

use with koa-router:

const Koa = require('koa');
const Subdomain = require('koa-subdomain');
const Router = require('koa-router');

const app = new Koa();
const subdomain = new Subdomain();
const router = new Router();

router.get('/', async ctx => {
  ctx.body = 'one';
});

// one.example.com
subdomain.use('one', router.routes());

app.use(subdomain.routes());
app.listen(8888);

more example:

const app = require('koa')();
const subdomain = require('koa-subdomain')();

// one.example.com
subdomain.use('one', router1);

// two.example.com
subdomain.use('two', router2);

subdomain
  .use('a.one', router3)  // a.one.example.com
  .use('b.one', router4); // b.one.example.com

// example.com
subdomain.use('', router5);

// *.example.com
subdomain.use('*', router6);

// *.one.example.com
subdomain.use('*.one', router7);

// one.*.example.com
subdomain.use('one.*', router8);

app.use(subdomain.routes());
app.listen(8888);

Wildcard subdomains will be accessible under wildcardSubdomains in the state of koa context.

const Koa = require('koa');
const Subdomain = require('koa-subdomain');
const Router = require('koa-router');

const app = new Koa();
const subdomain = new Subdomain();
const router1 = new Router();
const router2 = new Router();

// get test.example.com
router1.get('/', async ctx => {
    // in body will stand "test"
    ctx.body = ctx.state.wildcardSubdomains[0];
});

// get foo.bar.example.com
router2.get('/', async ctx => {
    // in body will stand "foo bar"
    ctx.body = ctx.state.wildcardSubdomains.join(' ');
});

// *.example.com
subdomain.use('*', router1.routes());
subdomain.use('*.*', router2.routes());

app.use(subdomain.routes());
app.listen(8888);

Note: Koa has a subdomainOffset setting (2, by default), so the domain of the app is assumed to be the last two parts of the host. Here is an example when it is useful: if your app domain is localhost:3000, you need to change subdomainOffset to 1 for proper subdomain detection.

const app = new Koa();

app.subdomainOffset = 1

// one.localhost:3000
subdomain.use('one', router);

koa1

Install:

npm install koa-subdomain@1 --save

Usage:

const app = require('koa')();
const subdomain = require('koa-subdomain')();
const router = require('koa-router')();

router.get('/', function * () {
  this.body = 'one';
});

// one.example.com
subdomain.use('one', router.routes());

app.use(subdomain.routes());
app.listen(8888);

Run test

git clone https://github.com/keenwon/koa-subdomain.git
cd koa-subdomain
npm install
npm test
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].