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

你可能感兴趣的文章
nodejs模块——fs模块
查看>>
Nodejs模块、自定义模块、CommonJs的概念和使用
查看>>
nodejs生成多层目录和生成文件的通用方法
查看>>
nodejs端口被占用原因及解决方案
查看>>
Nodejs简介以及Windows上安装Nodejs
查看>>
nodejs系列之express
查看>>
nodejs系列之Koa2
查看>>
Nodejs连接mysql
查看>>
nodejs连接mysql
查看>>
NodeJs连接Oracle数据库
查看>>
nodejs配置express服务器,运行自动打开浏览器
查看>>
NodeMCU教程 http请求获取Json中文乱码解决方案
查看>>
Nodemon 深入解析与使用
查看>>
NodeSession:高效且灵活的Node.js会话管理工具
查看>>
node~ http缓存
查看>>
node不是内部命令时配置node环境变量
查看>>
node中fs模块之文件操作
查看>>
Node中同步与异步的方式读取文件
查看>>
node中的get请求和post请求的不同操作【node学习第五篇】
查看>>
Node中的Http模块和Url模块的使用
查看>>