ES6 HttpApplication Middleware
const HttpRequest = function() {
this.query = ''
}
function HttpResponse() {
this.body = []
this.status = 0;
}
HttpResponse.prototype.write = function(block) {
this.body.push(block)
}
HttpResponse.prototype.display = function() {
console.log(this.body, this.status)
}
const HttpApplication = function() {
this.caches = []
}
HttpApplication.prototype.use = function(middleware) {
this.caches.push(middleware)
}
HttpApplication.prototype._createPipeLineItem = (fn, next) => {
return (req, rsp) => {
fn.call(this, req, rsp, next)
}
}
HttpApplication.prototype.applyRequest = function(req, rsp) {
const middleware = this.caches.map(item => item).reverse()
let lastHandler = (req, rsp) => {
rsp.write('404')
rsp.status = 404
}
for (let index = 0; index < middleware.length; index++) {
const element = middleware[index];
lastHandler = this._createPipeLineItem(element, lastHandler)
}
lastHandler(req, rsp)
}
const app = new HttpApplication()
app.use((req, rsp, next) => {
rsp.write('first middleware')
next(req, rsp)
rsp.write('first middleware end')
})
app.use((req, rsp, next) => {
rsp.write('second middleware')
rsp.status = 200
// next(req, rsp)
})
const req = new HttpRequest()
const rsp = new HttpResponse()
app.applyRequest(req, rsp)
rsp.display()
ES6 HttpApplication Middleware的更多相关文章
- webpack+react+redux+es6开发模式
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- 解读ASP.NET 5 & MVC6系列(6):Middleware详解
在第1章项目结构分析中,我们提到Startup.cs作为整个程序的入口点,等同于传统的Global.asax文件,即:用于初始化系统级的信息(例如,MVC中的路由配置).本章我们就来一一分析,在这里如 ...
- webpack+react+redux+es6
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(四)-- Middleware
本文记录了Asp.Net管道模型和Asp.Net Core的Middleware模型的对比,并在上一篇的基础上增加Middleware功能支持. 在演示Middleware功能之前,先要了解一下Asp ...
- Asp.Net Core WebApi学习笔记(四)-- Middleware
Asp.Net Core WebApi学习笔记(四)-- Middleware 本文记录了Asp.Net管道模型和Asp.Net Core的Middleware模型的对比,并在上一篇的基础上增加Mid ...
- Middleware详解
Middleware详解 在第1章项目结构分析中,我们提到Startup.cs作为整个程序的入口点,等同于传统的Global.asax文件,即:用于初始化系统级的信息(例如,MVC中的路由配置).本章 ...
- KoaHub.js:使用ES6/7特性开发Node.js框架(2)
介绍 KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架.可以直接在项目里使用 ES6/7(Generator Function, Class, Async ...
- KoaHub.js:使用ES6/7特性开发Node.js框架
KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架.可以直接在项目里使用 ES6/7(Generator Function, Class, Async & ...
- koa/redux middleware系统解析
middleware 对于现有的一些框架比如koa,express,redux,都需要对数据流进行一些处理,比如koa,express的请求数据处理,包括json.stringify,logger,或 ...
随机推荐
- php.ini优化,,,php-fpm
无论是apache还是nginx,php.ini都是合适的.而php-fpm.conf适合nginx+fcgi的配置. 1)打开PHP的安全模式 PHP的安全模式是个非常重要的PHP内嵌的安全机制,能 ...
- Extjs添加行双击事件
var grid = new Ext.grid.GridPanel({ store: store, trackMouseOver: false, disableSelection: true, aut ...
- 002-jdk10安装
下载地址: 1.百度云下载地址.(当然也可以官网下载,都一样) 地址:https://pan.baidu.com/s/13oZh_5tXb_Xadg9f-y2Idw 密码:a9h8 安装jdk: 2. ...
- XDU 1055
#include<stdio.h> #include<cstring> int main() { //freopen("orz.txt","w&q ...
- The 15th UESTC Programming Contest Preliminary H - Hesty Str1ng cdoj1551
地址:http://acm.uestc.edu.cn/#/problem/show/1551 题目: Hesty Str1ng Time Limit: 3000/1000MS (Java/Others ...
- Linux树莓派中/etc/rc.local不执行的问题
最近研究在树莓派中嵌入式开发java程序,并打算和Salesforce进行通信.需要开发一个java的web server,不想弄那么复杂,于是打算在linux系统中/etc/rc.local写想要执 ...
- IOS开发-数据库总结
关于数据存储概念: 数据结构: 基本对象:NSDictionary.NSArray和NSSet这些对象. 复杂对象:关系模型.对象图和属性列表多种结构等. 存储方式: 内存:内存存储是临时的,运行时有 ...
- Nginx访问控制_登陆权限的控制(http_auth_basic_module)
Nginx提供HTTP的Basic Auth功能,配置了Basic Auth之后,需要输入正确的用户名和密码之后才能正确的访问网站. 我们使用htpasswd来生成密码信息,首先要安装httpd-to ...
- hadoop cgroup+container配置
配置container-executor.cfg vim etc/hadoop/container-executor.cfg yarn.nodemanager.linux-container-exec ...
- 使用BusyBox制作根文件系统
1.BusyBox简介 BusyBox 是很多标准 Linux 工具的一个单个可执行实现.BusyBox 包含了一些简单的工具,例如 cat 和 echo,还包含了一些更大.更复杂的工具,例如 gre ...