npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

awix

v1.8.2

Published

a powerful web framework, build on module http2 and async/await.

Downloads

28

Readme

awix

基于HTTP/2模块和async/await关键字的web框架,支持功能:

  • 中间件
  • 路由分组/中间件分组
  • 解析Body数据
  • 限制请求数量
  • 限制指定IP的某一时间段内的最大请求数
  • 设置拒绝IP
  • 设置限制IP请求数的白名单
  • 中间件根据匹配规则执行
  • 守护进程模式
  • cluster集群
  • 全局日志
  • 显示负载情况

awix通过一个被称为请求上下文的对象打包了所有需要的请求信息,并包括了本次请求的stream实例。

安装

npm安装

#全局请使用 npm install -g awix
npm install awix

git方式

git clone此仓库,然后引入awix.js文件。

示例

'use strict';

const awix = require('awix');

/*
  在后续部分会详细讲解awix的选项,每个选项都会有默认值,不传递参数同样可以。
  在后续的示例中,如果不是十分必要,则会略过new awix()的过程,使用serv或app表示awix实例。
*/
var serv = new awix({
    //调试模式,默认为true
    //debug : false,

    /**
    * 设置单个IP2秒内最多请求100次。
    */
    maxIPRequest: 100,
    peerTime: 2,

    //设置每个进程同时处理请求的最大连接数。
    limit: 1000,

    //POST/PUT提交表单或上传文件时的最大数据,按照字节表示。
    bodyMaxSize: 10000000,

    //HTTPS密钥文件路径
    key : './localhost.key.pem',

    //HTTPS证书文件路径
    cert: './localhost.cert.pem',
});

var {router} = serv;

router.get('/' async rr => {
    //rr被称为请求上文对象,稍后会详细讲解。
    //设置rr.res.data会自动返回结果。
    rr.res.data = 'success';
});

//使用run接口只会启动一个进程,daemon会使用cluster集群。
serv.daemon(8118);

获取URL参数


serv.get('/test', async c => {
    //URL参数被解析到param属性中。
    c.res.data = c.param;
});

获取POST/PUT请求体数据

请求体数据,通常是表单,不过这不是绝对的,请求体数据格式在消息头的content-type字段描述。

content-type如果是application/x-www-form-urlencoded则表示是表单提交,也可以是其他类型,比如:

  • text/plain
  • text/json
  • text/xml
  • multipart/form-data

其中,multipart/form-data可以提交表单,但通常用来上传文件。

框架会自动解析application/x-www-form-urlencoded和multipart/form-data格式的数据,其他类型的则直接保存,不做处理。

解析后的数据在bodyparam字段,如果是上传文件,则会在files字段中保存。

获取表单数据


var {router} = serv;

router.post('/post-test', async c => {
  //返回解析后的数据,表单数据被解析成JSON对象的格式。
  c.res.data = c.bodyparam;
});

路由

路由就是根据域名后的路径去查找并执行对应的函数。框架本身路由的添加方式很简单,支持使用:表示变量,使用*匹配任意路径。并且路由参数只是以字符串形式解析,并不做各种类型转换的处理。解析后的参数保存在args字段。


/*
  name可以是任意字符串,访问形式:
    /page/index.html
    /page/a.html
*/
serv.get('/page/:name', async c => {
  //解析后的参数保存在c.args
  c.res.data = c.args['name'];
});

serv.get('/login/:username/:passwd', async c => {
  var {username, passwd} = c.args;
  //....
});

serv.options('/*', async c => {
  //接管所有的OPTIONS请求
  //...
});

中间件

中间件是框架提供的灵活强大的功能,依靠中间件可以方便的分离业务,组合完成复杂的功能,并且维护、替换都非常方便。

中间件基于洋葱模型实现,处理过程可以用下图描述:

按照这样的设计,后添加的中间件先执行,而在返回时则从内向外返回。其实就是函数调用栈结构。

编写中间件

中间件编写的参数有固定格式,执行下一层中间件也有固定写法。


/**
 * 使用add添加中间件,中间件一定是async声明的函数,
 * 接受两个参数,c是请求上下文,next表示下一层中间件。
 * 要执行则只需要await next(c)。
 * 如果检测发现不合法需要停止向内执行,则只需要不写await next(c)
 * 
 * add同时接受第二个参数,如果不填写则表示全局执行。
 * 
 * */
serv.add(async (c, next) => {
  c.res.data += 'I am middleware';
  await next(c);
  c.res.data += 'middleware end';
}, {preg: '/mid-test'});

serv.get('/mid-test', async c => {
  c.res.data += 'This test page for middleware';
});

访问/mid-test返回结果:


I am middleware
This test page for middleware
middleware end

使用add接口添加中间件,接受两个参数,第一个是请求上下文,第二个next表示下一层中间件。要执行则只需要

await next(c)

如果检测发现不合法需要停止向内执行,则只需要不写 await next(c)。

add支持第二个参数,如果没有表示全局执行,所有的请求都会先执行此中间件,否则可以填写值如下:

  • 字符串:表示组的名称,只在路由分组内添加中间件。

  • JSON对象:{preg: PREG, group: GROUP},preg表示匹配规则,group表示组名称,两个是可选项。preg的值如下:

    • 字符串:只对此路由执行。
    • 字符串数组:在其中的字符串都会执行。
    • 正则表达式:匹配后执行。
  • 正则表达式或字符串数组:其实就是preg的匹配规则。全局添加。