1 Star 0 Fork 0

zzy1661/BrowserCachePolicy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.js 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
Lucien 提交于 2016-06-06 07:54 . add etag
var PORT = 8888;
var url = require("url");
var http = require("http");
var path = require("path");
var fs = require('fs');
var config = require("./config.js");
var cache = require("./cache.js");
var mime = require("./mime").types;
var crypto = require('crypto');
var server = http.createServer(function (request, response) {
var pathname = url.parse(request.url).pathname;
var realPath = "assets" + pathname;
fs.exists(realPath, function (exists) {
if (!exists) {
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.write("This request URL " + pathname + " was not found on this server.");
response.end();
} else {
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
var contentType = mime[ext] || "text/plain";
cache(request, response, next);
function next() {
fs.readFile(realPath, "binary", function (err, file) {
console.log(new Date(), ":----read file-----");
if (err) {
response.writeHead(500, { 'Content-Type': contentType });
response.end(err);
} else {
response.writeHead(200, {
'Content-Type': contentType,
});
response.write(file, "binary");
response.end();
}
});
}
}
});
});
server.listen(PORT);
console.log("Server runing at port: " + PORT + ".");
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/zzy1661/BrowserCachePolicy.git
git@gitee.com:zzy1661/BrowserCachePolicy.git
zzy1661
BrowserCachePolicy
BrowserCachePolicy
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385