[JavaScript] 纯文本查看 复制代码
let http = require('http');
let path = require('path');
let fs = require('fs');
let { promisify} = require('util');
let stat = promisify(fs.stat);//获取文件状态信息
// 静态服务器
let url = require('url'); // 专门用来处理url路径的
let server = http.createServer(async function (req,res) {
let { pathname,query} = url.parse(req.url,true); // 就是将query转化成对象
let readPath = path.join(__dirname, 'public', pathname);//文件绝对路径
try {
let statObj = await stat(readPath);
// 和客户端说 10m内走缓存
res.setHeader('Cache-Control','max-age=10'); //1.1
res.setHeader('Expires',new Date(Date.now()+10*1000).toGMTString()); //1.0
if (statObj.isDirectory()) {
let p = path.join(readPath, 'index.html');
await stat(p);//判断有没有这文件,如果读不到,就报错,就被catch到
// 如果当前目录下有html那么就返回这个文件
fs.createReadStream(p).pipe(res);
} else {
// 是文件 读取对应的文件直接返回即可
fs.createReadStream(readPath).pipe(res);
}
}catch(e){
res.statusCode = 404;
res.end(`Not found`);
}
}).listen(3000);
[JavaScript] 纯文本查看 复制代码
let server = http.createServer(async function (req,res) {
let { pathname,query} = url.parse(req.url,true);
let readPath = path.join(__dirname, 'public', pathname);
try {
let statObj = await stat(readPath);
res.setHeader('Cache-Control','no-cache');
if (statObj.isDirectory()) {
let p = path.join(readPath, 'index.html');
let statObj = await stat(p);
res.setHeader('Last-Modified', statObj.ctime.toGMTString());//服务器设置文件最后修改时间
if (req.headers['if-modified-since'] === statObj.ctime.toGMTString()){//客户端请求的时间
res.statusCode = 304;
res.end();
return; // 走缓存
}
fs.createReadStream(p).pipe(res);
} else {
res.setHeader('Last-Modified', statObj.ctime.toGMTString());
if (req.headers['if-modified-since'] === statObj.ctime.toGMTString()) {
res.statusCode = 304;
res.end();
return; // 走缓存
}
fs.createReadStream(readPath).pipe(res);
}
}catch(e){
res.statusCode = 404;
res.end(`Not found`);
}
}).listen(3000);
[JavaScript] 纯文本查看 复制代码
let http = require('http');
let path = require('path');
let fs = require('fs');
let { promisify} = require('util');
let stat = promisify(fs.stat);
let url = require('url');
let crypto = require('crypto');
let server = http.createServer(async function (req,res) {
let { pathname,query} = url.parse(req.url,true);
let readPath = path.join(__dirname, 'public', pathname);
try {
let statObj = await stat(readPath);
res.setHeader('Cache-Control','no-cache');
if (statObj.isDirectory()) {
let p = path.join(readPath, 'index.html');
let statObj = await stat(p);
// 我要根据文件内容 生成一个md5的摘要 最耗性能 ,给实体加一个标签
let rs = fs.createReadStream(p);//读流
let md5 = crypto.createHash('md5'); // 不能写完相应体在写头
let arr = [];
rs.on('data',function (data) {
md5.update(data);//读一点加密一点
arr.push(data);//不能res.write(),下面setHeader还没完成
});
rs.on('end',function () {
let r = md5.digest('base64');
res.setHeader('Etag', r);//服务器设置Etag 和 客户端 if-none-match最对比
if (req.headers['if-none-match'] === r ){
res.statusCode = 304;
res.end();
return;
}
res.end(Buffer.concat(arr));
})
} else {
let rs = fs.createReadStream(readPath);
let md5 = crypto.createHash('md5'); // 不能写完相应体在写头
let arr = [];
rs.on('data', function (data) {
md5.update(data);
arr.push(data);
});
rs.on('end', function () {
let r = md5.digest('base64');
res.setHeader('Etag', r);
if (req.headers['if-none-match'] === r) {
res.statusCode = 304;
res.end();
return;
}
res.end(Buffer.concat(arr));
})
}
}catch(e){
res.statusCode = 404;
res.end(`Not found`);
}
}).listen(3000);