awy2

2.1.0 • Public • Published

awy框架

awy2是一个基于Node.js的HTTP/2模块开发的Web框架,基于async/await关键字。

支持功能

  • 中间件
  • 路由
  • 路由分组
  • 中间件按照路由规则匹配执行
  • 解析Body数据
  • 解析上传的文件
  • 启用守护进程模式
  • 配置HTTPS

使用示例

 
const awy = require('awy2');
 
var ar = new awy();
 
ar.config.https_on = true;
ar.config.https_options.cert = '../rsa/localhost-cert.pem';
ar.config.https_options.key = '../rsa/localhost-privkey.pem';
 
/*
一定要注意的是:
    回调函数要写成async rr的形式。
rr是打包了request和response的对象:
    {
        req,
        res
    }
    
*/
 
ar.get('/', async rr => {
    rr.res.Body = 'success';        
});
 
ar.run('localhost', 8080);
 

curl 'http://localhost:8080/'

输出结果:
  success

获取URL参数(QueryString参数)

 
const awy = require('awy2');
 
var ar = new awy();
 
ar.config.https_on = true;
ar.config.https_options.cert = '../rsa/localhost-cert.pem';
ar.config.https_options.key = '../rsa/localhost-privkey.pem';
 
ar.get('/test', async rr => {
    var {name} = rr.req.Param;
    console.log(name);
    rr.res.Body = name;
});
 
ar.run('localhost', 8080);
 

curl 'http://localhost:8080/test?name=helo'

输出结果:
  helo

获取POST提交的数据

 
const awy = require('awy');
 
var ar = new awy();
 
ar.config.https_on = true;
ar.config.https_options.cert = '../rsa/localhost-cert.pem';
ar.config.https_options.key = '../rsa/localhost-privkey.pem';
 
ar.post('/pt', async rr => {
    var {username} = rr.req.BodyParam;
    rr.res.Body = username;
});
 
ar.run('localhost', 8080);
 

curl 'http://localhost:8080/pt' -d 'username=albert'

返回结果:
  albert

完整文档

Package Sidebar

Install

npm i awy2

Weekly Downloads

0

Version

2.1.0

License

MIT

Unpacked Size

33.6 kB

Total Files

3

Last publish

Collaborators

  • ant-army