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,或 ...
随机推荐
- Zen-cart产品页面随机调用Wordpress文章
<?php require('./wordpress所在目录/wp-blog-header.php'); ?><?php$rand_posts = get_posts('number ...
- Linux系统——文件系统与LVM 逻辑
格式化命令 mkfs. mkswap mkfs格式化数据磁盘 # mkfs -t ext4 /dev/sdb1 # mkfs.ext4 /dev/sdb1 -t 指定格式化文件类型 -b 指定bloc ...
- HDU 2191 珍惜现在,感恩生活(多重背包模板题)
多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...
- Mybatis中的#与$的区别
一.对比场景 场景:数据库分表时,需要将分表的表序号传入的sql中. SpringBoot中使用注解如下: @Insert("insert into collect_#{tblNum}(id ...
- Java 创建数组的方式, 以及各种类型数组元素的默认值
①创建数组的方式3种 ①第1种方法 public class MyTest { public static void main(String[] args){ //method 1 int[] arr ...
- FFmpeg 入门(2):输出视频到屏幕
本文转自:FFmpeg 入门(2):输出视频到屏幕 | www.samirchen.com SDL 我们这里使用 SDL 来渲染视频到屏幕.SDL 是 Simple Direct Layer 的缩写, ...
- 高通camera结构(摄像头基础介绍)
摄像头基础介绍 一.摄像头结构和工作原理. 拍摄景物通过镜头,将生成的光学图像投射到传感器上,然后光学图像被转换成电信号,电信号再经过模数转换变为数字信号,数字信号经过DSP加工处理,再被送到电脑中进 ...
- Django学习笔记之uWSGI详解
WSGI是什么? WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义 ...
- 20145321 《Java程序设计》第9周学习总结
20145321 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 16.1 JDBC 1.JDBC简介: JDBC是Java联机数据库的标准规范,它定义一组标准 ...
- 20145328 《Java程序设计》第2周学习总结
20145328 <Java程序设计>第2周学习总结 教材学习内容总结 掌握了上周没有学会的IDEA的用法 掌握了一些快捷键用法,在用IDEA编写程序的过程中的体验比直接使用cmd进行编写 ...