博客
关于我
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/

你可能感兴趣的文章
onCreate()方法中的参数Bundle savedInstanceState 的意义用法
查看>>
OneBlog Shiro 反序列化漏洞复现
查看>>
one_day_one--mkdir
查看>>
ONI文件生成与读取
查看>>
onlyoffice新版5.1.2版解决中文汉字输入重复等问题
查看>>
oobbs开发手记
查看>>
OPEN CASCADE Curve Continuity
查看>>
Open vSwitch实验常用命令
查看>>
Open WebUI 忘了登入密码怎么办?
查看>>
open-vm-tools-dkms : 依赖: open-vm-tools (>= 2:9.4.0-1280544-5ubuntu3) 但是它将不会被安装
查看>>
Openbox-桌面图标设置
查看>>
opencart出现no such file or dictionary
查看>>
opencv Mat push_back
查看>>
opencv waitKey() 函数理解及应用
查看>>
OpenCV 中的图像转换
查看>>
OpenCV 在 Linux 上的 python 与 anaconda 无法正常工作.收到未实现 cv2.imshow() 的错误
查看>>
Opencv 完美配置攻略 2014 (Win8.1 + Opencv 2.4.8 + VS 2013)上
查看>>
opencv 模板匹配, 已解决模板过大程序不工作的bug
查看>>
opencv&Python——多种边缘检测
查看>>
opencv&python——高通滤波器和低通滤波器
查看>>