通用中间件 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

从界面到网络空间

从界面到网络空间

(美)海姆 / 金吾伦/刘钢 / 上海科技教育出版社 / 2000-7 / 16.40元

计算机急剧改变了20世纪的生活。今天,我们凭借遍及全球的计算机网络加速了过去以广播、报纸和电视形式进行的交流。思想风驰电掣般在全球翻飞。仅在角落中潜伏着已完善的虚拟实在。在虚拟实在吕,我们能将自己沉浸于感官模拟,不仅对现实世界,也对假想世界。当我们开始在真实世界与虚拟世界之间转换时,迈克尔·海姆问,我们对实在的感觉如何改变?在〈从界面到网络空间〉中,海姆探讨了这一问题,以及信息时代其他哲学问题。他......一起来看看 《从界面到网络空间》 这本书的介绍吧!

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

多种字符组合密码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具