通用中间件 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://codercto.com/soft/d/3499.html

人件

人件

Tom DeMarco、Timothy Lister / UML China / 清华大学出版社 / 2003-6 / 35.00元

《人件(第2版)》专门讨论了软件开发和维护的团队管理问题,并向人们的传统认识提出了挑战。作者汤姆·迪马可,蒂姆·李斯特在书中推崇人本管理思想,指出知识型企业的核心是人,而不是技术。《人件(第2版)》于1987年首次出版后,曾在西方引起了轰动,被誉为“对美国软件业影响最大的一本书”。《人件(第2版)》还对大中型组织中的软件开发团队如何运作进行了深入探讨。《人件》已成为软件图书中的经典之作。它和《人月......一起来看看 《人件》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具