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

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

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/

你可能感兴趣的文章
NIFI集群_内存溢出_CPU占用100%修复_GC overhead limit exceeded_NIFI: out of memory error ---大数据之Nifi工作笔记0017
查看>>
NIFI集群_队列Queue中数据无法清空_清除队列数据报错_无法删除queue_解决_集群中机器交替重启删除---大数据之Nifi工作笔记0061
查看>>
NIH发布包含10600张CT图像数据库 为AI算法测试铺路
查看>>
Nim教程【十二】
查看>>
Nim游戏
查看>>
NIO ByteBuffer实现原理
查看>>
Nio ByteBuffer组件读写指针切换原理与常用方法
查看>>
NIO Selector实现原理
查看>>
nio 中channel和buffer的基本使用
查看>>
NIO三大组件基础知识
查看>>
NIO与零拷贝和AIO
查看>>
NIO同步网络编程
查看>>
NIO基于UDP协议的网络编程
查看>>
NIO笔记---上
查看>>
NIO蔚来 面试——IP地址你了解多少?
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NISP国家信息安全水平考试,收藏这一篇就够了
查看>>
NIS服务器的配置过程
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>