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

你可能感兴趣的文章
Pandas模块,我觉得掌握这些就够用了!
查看>>
Pandas玩转文本处理!
查看>>
SpringBoot 整合 Mybatis Plus 实现基本CRUD功能
查看>>
pandas的to_sql方法中使用if_exists=‘replace‘
查看>>
Springboot ppt转pdf——aspose方式
查看>>
pandas读取csv编码utf-8报错
查看>>
pandas读取parquet报错
查看>>
pandas读取数据用来深度学习
查看>>
pandas读取文件时,不去掉前面的0 保留原有的数据格式
查看>>
Pandas进阶大神!从0到100你只差这篇文章!
查看>>
spring5-介绍Spring框架
查看>>
pandas,python - 如何在时间序列中选择特定时间
查看>>
Spring 框架之 AOP 原理深度剖析
查看>>
Pandas:如何按列元素的组合分组,以指示基于不同列的值的同现?
查看>>
Pandas:将一列与数据帧的所有其他列进行比较
查看>>
PANDA和GLOB:将文件夹中的所有xlsx文件转换为CSV类型错误:__init__()获得意外的关键字参数‘;xfid‘;
查看>>
panda查找想要找的行合并成一个新pd
查看>>
PANDA:基于多列对数据表的行运行计算,并将输出存储在新列中
查看>>
PandoraFMS 监控软件 SQL注入漏洞复现
查看>>
PandoraFMS 监控软件 任意文件上传漏洞复现
查看>>