0.0.7 • Published 7 years ago
rune-server v0.0.7
server
Simple server based on koa.
Usage
Basic
const Server = require('..');
const server = new Server(11011);
server.router.get('/hello', ctx => ctx.body = 'hello');
server.run();
Router
const Server = require('..');
const server = new Server({
port: 11011,
cors: {},
serve: './dist',
bodypaser: { jsonLimit: '50mb', formLimit: '50mb' }
});
async function asyncFun() {
return 'hello';
}
// 异步
server.router.get('/hello', async ctx => {
ctx.body = await asyncFun();
});
// 错误时返回状态码
server.router.get('/coffee', async ctx => {
ctx.throw(418);
});
server.run();
Router Attach
const Server = require('..');
const server = new Server({
routes: { 'admin': '/path/to/admin' },
port: 9999,
});
server
.attach({
'GET admin': (ctx) => ctx.body = 'Hi, admin',
'GET /guest': (ctx) => ctx.body = 'Hi, guest',
}, (method, name, handler) => {
console.log(method, name);
return handler;
})
.bind('POST admin', (ctx) => ctx.body = 'Add user')
.run();