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

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

Express ?????

?????????

??????????????????????JavaScript ?????????????????????????????????

function static(root, options = {}) {    const {         dotfiles = "ignore",        etag = true,        lastModified = true,        maxAge = 0,        setHeaders    } = options;        return function (req, res, next) {        const { pathname } = url.parse(req.url, true);        const file = path.join(root, pathname);        const parts = file.split(path.sep);        const 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, (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, (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 ?????????????????????????????????????

1. JSON ???

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

2. URL-encoded ???

function urlencoded(options) {    const { extended } = options;    return function (req, res, next) {        const contentType = req.headers["content-type"];        if (contentType === "application/x-www-form-urlencoded") {            const buffer = [];            req.on("data", (data) => {                buffer.push(data);            });            req.on("end", () => {                const result = buffer.toString();                if (extended) {                    req.body = qs.parse(result);                } else {                    req.body = querystring.parse(result);                }                next();            });        } else {            next();        }    };}

3. ?????

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", (data) => {                buffer.push(data);            });            req.on("end", () => {                const r = Buffer.concat(buffer);                if (charset === "gbk") {                    req.body = iconv.decode(r, charset);                } else {                    req.body = buffer.toString();                }                next();            });        } else {            next();        }    };}

???????? Express ????????????????????????????

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

你可能感兴趣的文章
MySQL锁与脏读、不可重复读、幻读详解
查看>>
MySQL集群解决方案(4):负载均衡
查看>>
mysql颠覆实战笔记(八)--mysql的自定义异常处理怎么破
查看>>
MySQL高级-MySQL并发参数调整
查看>>
MySQL高级-视图
查看>>
MySQL:判断逗号分隔的字符串中是否包含某个字符串
查看>>
Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
查看>>
Nacos安装教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Nacos配置中心集群原理及源码分析
查看>>
nacos配置自动刷新源码解析
查看>>
Nacos集群搭建
查看>>
nacos集群搭建
查看>>
Navicat for MySQL 查看BLOB字段内容
查看>>
Neo4j电影关系图Cypher
查看>>
Neo4j的安装与使用
查看>>
Neo4j(2):环境搭建
查看>>
Neo私链
查看>>
nessus快速安装使用指南(非常详细)零基础入门到精通,收藏这一篇就够了
查看>>
Nessus漏洞扫描教程之配置Nessus
查看>>
Nest.js 6.0.0 正式版发布,基于 TypeScript 的 Node.js 框架
查看>>