
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Asop
是为了适应 非 web
开发而 fork 自 Koa
项目而实现的。
Asop
是一个十分具有表现力的中间件框架,力求让应用开发和 API 使用更加地愉快。Asop 的中间件之间按照编码顺序在栈内依次执行,允许您执行操作并向下传递请求(downstream),之后过滤并逆序返回响应(upstream)。
Asop
没有捆绑任何中间件,也不依赖第三方包,实现代码不超过120行,可以运行在 Node.js环境 和 浏览器端。
下面例子均以 Node.js 环境为准。
async 函数
,或者使用 Promise
。$ npm install asop
<script src="https://cdn.jsdelivr.net/npm/asop@0.0.2/dist/asop.umd.js"></script>
const Asop = require('asop');
const app = new Asop();
// 使用一个普通函数作为中间件
app.use((ctx, next) => {
ctx.hello = 'hello';
return next();
});
// 使用 async 函数作中间件
app.use(async (ctx, next) => {
ctx.hello += ' world!';
await next();
});
const handle = app.callback();
const done = (ctx) => {
console.log(ctx.hello);
};
handle(done);
// => 'hello world!'
Asop 是一个中间件框架,可以采用两种不同的方法来实现中间件:
以下是使用两种不同方法实现一个日志中间件的示例:
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
console.log(`Take ${ms}ms`);
});
// 中间件通常带有两个参数 (ctx, next), ctx 是一个请求的上下文(context),
// next 是调用执行下游中间件的函数. 在代码执行完成后通过 then 方法返回一个 Promise.
app.use((ctx, next) => {
const start = Date.now();
return next().then(() => {
const ms = Date.now() - start;
console.log(`Take ${ms}ms`);
});
});
每个中间件都接收一个纯对象 Object
,该对象初始化状态仅仅包含了 Asop 实例的下面两个简单配置为属性。 ctx
通常用作上下文对象的参数名称。
process.env.NODE_ENV || 'development'
;app.use(async (ctx, next) => {
await next();
});
在执行 new Asop()
时创建的对象被称为 Asop 应用对象。
应用对象是不带有任何服务的 Asop 接口,它可以处理中间件的注册,通过 callback 将执行中间件,进行默认错误处理,以及对上下文对象进行配置。
了解有关应用程序对象的更多信息请到 应用 API 参考.
$ npm test
FAQs
middleware framework for node.js
We found that asop demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.