通用中间件 Middl

码农软件 · 软件分类 · 服务器端JavaScript · 2019-04-13 15:43:34

软件介绍

Middl 是采用JS编写的通用中间件。

安装

npm install --save middl

模块使用:

const middl = require('middl');
const app = middl();

// a sync middleware
app.use((input, output) => {
    output.prop = 1;
});

// an async middleware
app.use((input, output) => {
    // Faking a time consuming task...
    return new Promise(resolve => {
        setTimeout(() => {
            output.prop += 1;
            resolve();
        }, 10);
    });
});

// a time measuring logger:
app.use((input, output) => {
    var start = new Date();
  next()
        .then(() => {
            var ms = new Date() - start;
            console.log('Done in %s ms', ms);
        });
});
// or even prettier with generator functions:
app.use(function *(input, output) {
    var start = new Date();
    yield next();
    var ms = new Date() - start;
    console.log('Done in %s ms', ms);
});
// or when using Babel and async/await:
app.use(async (input, output) => {
    var start = new Date();
  await next();
    var ms = new Date() - start;
    console.log('Done in %s ms', ms);
});

// pass in the initial `input` and `output` objects
// and run the middleware stack:
app.run({val: 'hello'}, {})
    .then(output => {
        // output.prop === 2
    });

示例代码:

const http = require('http');
const middl = require('middl');

// Make middl more Express like by using `url` as the property to match paths with:
const app = middl({pathProperty: 'url'});

// Adding all app.METHOD() functions à la Express:
http.METHODS.forEach(method => {
    app[method.toLowerCase()] = app.match({method});
});
// Also the app.all():
app.all = (path, fn) => {
    http.METHODS.forEach(method => {
        app[method.toLowerCase()](path, fn);
    });
    return app;
};

// A route handler for requests to: GET /test
app.get('/test', (req, res) => {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('ok\n');
});
// A route handler for requests to: POST /test
app.post('/test', (req, res) => {
    res.writeHead(202, {'Content-Type': 'text/plain'});
    res.end('accepted\n');
});

// Make the middle app web server listen on port 3000:
http.createServer(app).listen(3000);

本文地址:https://www.codercto.com/soft/d/3499.html

蚂蚁金服

蚂蚁金服

由曦 / 中信出版集团股份有限公司 / 2017-4-7 / CNY 59.00

在中国,支付宝(其母公司为蚂蚁金服)是一个家喻户晓的品牌。我们在用手机扫码支付,或者用余额宝理财的时候,一定会和支付宝发生关系。但是很多人不知道,支付宝的母公司叫作“蚂蚁金服”。蚂蚁金服不仅有支付宝,还有余额宝、网商银行、芝麻信用等一系列产品和服务。成立于2004年、起始于支付宝的蚂蚁金服集团,如今已经是全球估值最高的科技金融企业。然而,在成立之初,它只是淘宝网的结算部门,员工只有区区几人,记账用......一起来看看 《蚂蚁金服》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试