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

你可能感兴趣的文章
spring5-介绍Spring框架
查看>>
pandas,python - 如何在时间序列中选择特定时间
查看>>
Spring 框架之 AOP 原理深度剖析
查看>>
Pandas:如何按列元素的组合分组,以指示基于不同列的值的同现?
查看>>
Pandas:将一列与数据帧的所有其他列进行比较
查看>>
PANDA:基于多列对数据表的行运行计算,并将输出存储在新列中
查看>>
PandoraFMS 监控软件 SQL注入漏洞复现
查看>>
PandoraFMS 监控软件 任意文件上传漏洞复现
查看>>
PanTools多网盘登录神器
查看>>
Papyrus项目常见问题解决方案
查看>>
Parallel.ForEach使用示例
查看>>
Parallel.ForEach的基础使用
查看>>
parallels desktop for mac安装虚拟机 之parallelsdesktop密钥 以及 parallels desktop安装win10的办公推荐可以提高办公效率...
查看>>
parallelStream导致LinkedList遍历时空指针的问题
查看>>
Parameter ‘password‘ not found. Available parameters are [md5String, param1, username, param2]
查看>>
ParameterizedThreadStart task
查看>>
Paramiko exec_命令的实时输出
查看>>
Spring security之管理session
查看>>
paramiko模块
查看>>
param[:]=param-lr*param.grad/batch_size的理解
查看>>