博客
关于我
express 的中间件
阅读量:88 次
发布时间:2019-02-25

本文共 3471 字,大约阅读时间需要 11 分钟。

static

static 中间件主要用来提供静态资源服务 ,可以设置静态资源的目录 设置响应头 及缓存等等配置,代码如下

function static(root,options = {   }){       let {    dotfiles = "ignore" ,etag=true,lastModified,maxAge=0, setHeaders } = options;    return function(req,req,next){           let {    pathname } = url.parse(req.url,true);        let file = path.join(root,pathname);        let parts = file.split(path.sep);        let isDotFile = parts[parts.length-1][0]==".";        if(isDotFile && dotfiles=="deny"){     //拒绝访问.文件            res.setHeader("Content-Type","text/html");            res.statusCode = 403;            return res.end(http.STATUS_CODES[403]);        }        fs.stat(file,function(error,stat){               if(error){                   next()            }else{                   if(etag){                       res.setHeader("ETag",stat.mtime.toLocaleDateString());                }                if(lastModified){                       res.setHeader("Last-Modified",stat.mtime.toUTCString())                }                if(typeof setHeaders =='function'){                       setHeaders(req,req,function(params){                           console.log(params);                    });                }                res.setHeader("Cache-Control",`max-age=${     maxAge}`);                res.setHeader("Content-Type",mime.getType(file))                 fs.createReadStream(file).pipe(res);            }        })    }};

bodyParser

bodyParser 中间件用来解析请求参数

json

function json(options){       return function(req,res,next){           let contentType = req.headers["content-type"];        if(contentType=="application/json"){               const buffer = [];            req.on("data",function(data){                   buffer.push(data);            });            req.on("end",function(){                   let result = buffer.toString();                req.body = JSON.parse(result);                next()            })        }else{               next();        }    }}

urlencoded

function urlencoded(options){       let {    extended } = options;    return function(req,res,next){           let contentType = req.headers["content-type"];        console.log(contentType);        if(contentType=="application/x-www-form-urlencoded"){               const buffer = [];            req.on("data",function(data){                   buffer.push(data);            });            req.on("end",function(){                   let result = buffer.toString();                if(extended){                       //qs 可以支持嵌套对象;                    req.body = qs.parse(result);                }else{                       req.body = querystring.parse(result);                }                next();            })        }else{               next();        }    }}

text

function text(options){       return function(req,res,next){           const contentType = type.parse(req.headers["content-type"]);        const charset =  contentType.parameters.charset;        const cType = contentType.type;        if(cType=="text/plain"){               const buffer = [];            req.on("data",function(data){                   buffer.push(data);            });            req.on("end",function(){                   let r = Buffer.concat(buffer);                if(charset=="gbk"){                       req.body =  iconv.decode(r,charset);                }else{                       req.body = buffer.toString();                }                next()            })        }else{               next();        }    }}

转载地址:http://tld.baihongyu.com/

你可能感兴趣的文章
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>