1.1.20 • Published 5 years ago

sv_node_websrv v1.1.20

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

sv_node_websrv

node web service with koa

安装

需要node v7.6.0以上版本支持

npm set registry http://npm.scsv.online

npm adduser --registry http://npm.scsv.online
$ Username: scsv
$ Password: scsv
$ Email: scsv@scsv.online

npm install sv_node_websrv --save

使用示例

const websrv = require('sv_node_websrv')('/example/');
const debug = require('debug')('websrv:example');

//加载中间件
websrv.use(async(ctx, next)=> {
    debug('hello');
    await next();
});

//路由
websrv.router.get('test', async(ctx, next) =>{
    debug('route test ok');
    JR(ctx, 'route test ok\n');
});

//消息响应过滤器
websrv.filter(async(ctx, next)=>{
   debug('filter output:', ctx.status, ctx.body);
});

//启动服务
websrv.start(9000);


//每分钟上报性能报告
setInterval(()=>
    {
        status_report(websrv.perf.value());
    },
    60000);

请求

curl http://localhost:9000/example/test

输出:

[2017-07-12 10:51:59.086] [INFO] websrv - Server start, listening on port 9000
[2017-07-12 10:52:02.120] [INFO] websrv - GET /example/test
  websrv hello +3s
  websrv route test ok +5ms
[2017-07-12 10:52:02.128] [INFO] websrv - GET /example/test: use 9ms
[2017-07-12 10:52:59.066] [INFO] websrv:perf - { tpmc: 37, input: 0, output: 238, art: '0.3' }

中间件

websrv.use(async(ctx, next)=> {
    debug('hello');
    await next();
});

路由

main.js

require('router')(websrv.router);

router.js

module.exports =  (router) => {
    router
        .get('a', func_a)
        .post('b/:id', func_b)
        .put('c/:id', func_c)
        .del('d/:id', func_d)
}

系统依赖组件