有时候,咱们想读取一些服务器上的文件,可是又不想写太复杂的程序,能够考虑用nodejs,能够很简单的写出一个文件服务器
下面是我写的一个简单的文件服务器,能读取文件夹信息,附带缓存功能,这是github连接,或者直接复制下面的代码运行便可,须要安装mime的依赖javascript
const port = 3004; // 端口号 const http = require('http'); const url = require('url'); const fs = require('fs'); const path = require('path'); const mime = require('mime'); const STATIC_FOLDER = 'public'; // 默认读取public文件夹下的文件 const IS_OPEN_CACHE = true; // 是否开启缓存功能 const CACHE_TIME = 10;// 告诉浏览器多少时间内能够不用请求服务器,单位:秒 const server = http.createServer((req, res) => { let reqUrl = decodeURIComponent(req.url); // 中文解码 const obj = url.parse(reqUrl); // 解析请求的url let pathname = obj.pathname; // 请求的路径 if (pathname === '/') { pathname = './index.html'; } const realPath = path.join(__dirname, STATIC_FOLDER, pathname); // 获取物理路径 // 获取文件基本信息,包括大小,建立时间修改时间等信息 fs.stat(realPath, (err, stats) => { let endFilePath = '', contentType = '';; if (err || stats.isDirectory()) { // 报错了或者请求的路径是文件夹,则返回404 res.writeHead(404, 'not found', { 'Content-Type': 'text/plain' }); res.write(`the request ${pathname} is not found`); res.end(); } else if (stats.isDirectory()) { // 读取文件夹 fs.readdir(realPath, { encoding: 'utf8' }, (err, files) => { res.statusCode = 200; res.setHeader('content-type', 'text/html'); let result = ''; for (let i = 0; i < files.length; i++) { result += `<a href="${req.url}/${files[i]}">${files[i]}</a><br/>`; } let html = ` <!doctype html> <html> <head> <meta charset='utf-8'/> </head> <body> ${result} </body> </html>`; res.end(html); }); } else { // 读取文件 let ext = path.extname(realPath).slice(1); // 获取文件拓展名 contentType = mime.getType(ext) || 'text/plain'; endFilePath = realPath; if (!IS_OPEN_CACHE) { // 未开启缓存 let raw = fs.createReadStream(endFilePath); res.writeHead(200, 'ok'); raw.pipe(res); } else { // 获取文件最后修改时间,并把时间转换成世界时间字符串 let lastModified = stats.mtime.toUTCString(); const ifModifiedSince = 'if-modified-since'; // 告诉浏览器在规定的什么时间内能够不用请求服务器,直接使用浏览器缓存 let expires = new Date(); expires.setTime(expires.getTime() + CACHE_TIME * 1000); res.setHeader("Expires", expires.toUTCString()); res.setHeader('Cache-Control', 'max-age=' + CACHE_TIME); if (req.headers[ifModifiedSince] && lastModified === req.headers[ifModifiedSince]) { // 请求头里包含请求ifModifiedSince且文件没有修改,则返回304 res.writeHead(304, 'Not Modified'); res.end(); } else { // 返回头Last-Modified为当前请求文件的最后修改时间 res.setHeader('Last-Modified', lastModified); // 返回文件 let raw = fs.createReadStream(endFilePath); res.writeHead(200, 'ok'); raw.pipe(res); } } } }); }); server.listen(port); console.log(`server is running at http://localhost:${port}`);
不过目前还有一点问题,服务器缓存返回304,还有修改文件后,再次请求会返回最新文件这个功能目前没有问题,不过设置的Cache-Control和Expires后,在规定的时间内仍是会请求服务器,这个还须要再看一下怎么回事,要是有人了解的话能够告诉我一下,谢谢。html