Node.js学习之静态资源服务器

因为 fs.createReadStream 尝试读取文件夹,需要兼容下访问路径是文件夹的时候,返回一个目录页,也就是在 fs.access 之后判断文件类型
支持文件夹预览
当访问的路径是文件夹的时候程序会报错
模块化可定制的静态资源服务器理想的使用方式应该是这样的
执行 npm run test 可以测试fs.access(filePath, fs.constants.R_OK, err => { if (err) { res.writeHead(404, { 'content-type': 'text/html', }); res.end('文件不存在!'); } else { const stats = fs.statSync(filePath); const list = []; if (stats.isDirectory()) { // 如果是文件夹则遍历文件夹,生成改文件夹内的文件树 // 遍历文件内容,生成 html } else { res.writeHead(200, { 'content-type': mime.contentType(path.extname(url)), }); fs.createReadStream(filePath).pipe(res); } } });
遍历生成 html 部分需要用到 文件夹操作 章节介绍的知识,为了方便生成 HTML,demo 使用了 Handlebar 模板引擎,主要逻辑
const http = require('http'); const fs = require('fs'); const path = require('path'); const mime = require('mime-types'); const defaultConf = require('./config'); class StaticServer { constructor(options = {}) { this.config = Object.assign(defaultConf, options); } start() { const { port, root } = this.config; this.server = http.createServer((req, res) => { const { url, method } = req; if (method !== 'GET') { res.writeHead(404, { 'content-type': 'text/html', }); res.end('请使用 GET 方法访问文件!'); return false; } const filePath = path.join(root, url); fs.access(filePath, fs.constants.R_OK, err => { if (err) { res.writeHead(404, { 'content-type': 'text/html', }); res.end('文件不存在!'); } else { res.writeHead(200, { 'content-type': mime.contentType(path.extname(url)), }); fs.createReadStream(filePath).pipe(res); } }); }).listen(port, () => { console.log(`Static server started at port ${port}`); }); } stop() { this.server.close(() => { console.log(`Static server closed.`); }); } } module.exports = StaticServer;完整代码:https://github.com/Samaritan89/static-server/tree/v1
在创建 HTTP 服务器实现了一个最简单的静态资源服务器,可以对代码进行写改造,增加文件夹预览功能,暴露出一些配置,变成一个可定制的静态资源服务器模块
const StaticServer = require('YOUR_STATIC_SERVER_FILE_PATH');
const staticServer = new StaticServer({
port: 9527,
root: '/public',
});
staticServer.start();
staticServer.close();
这样的使用方式就要求代码实现模块化,Node.js 实现一个模块非常简单

同样在项目根目录执行 npm run test ,使用浏览器访问 127.0.0.1:9527 可以看到目录文件的展示
完整代码:https://github.com/Samaritan89/static-server/tree/v2
通过 git 代码修改记录可以清晰看到本次的变更:https://github.com/Samaritan89/static-server/commit/5565788dc317f29372f6e67e6fd55ec92323d0ea

更多编程相关知识,请访问:编程教学!!
以上就是Node.js学习之静态资源服务器的详细内容,更多请关注聚合云库其它相关文章!
相关热词: 服务器
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/javascript/4526.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
那么问题来了:我们要怎
时间:2021-01-20
-
2021年,进修JavaScript必然要
时间:2021-01-20
-
DeFi(去中心化金融)现在的
时间:2021-01-20
-
您无需对类型做任何假设
时间:2021-01-20
-
例如下面这句: export co
时间:2021-01-20
-
key ); } console.log(hasOwn({
时间:2021-01-20
-
是不是? 自定义 Hook 如果你
时间:2021-01-20
-
Javascript是什么?
时间:2021-01-04
热门文章
-
连续3年稳居第一,全球1240万用户,Java
时间:2021-01-04
-
一篇带给你JavaScript的Class语法介绍
时间:2021-01-04
-
key ); } console.log(hasOwn({ name :1}
时间:2021-01-20
-
深入理解JavaScript中的箭头函数
时间:2021-01-04
-
Javascript在Chrome浏览器中调试的七个步骤
时间:2021-01-04
-
那么问题来了:我们要怎么在样式中使用
时间:2021-01-20
-
Canvas入门实战之实现一个图形验证码
时间:2021-01-04
-
详解js异步文件加载器
时间:2021-01-04
-
是不是? 自定义 Hook 如果你想仿照 useSta
时间:2021-01-20
-
复盘Node项目中遇到的13+常见问题和解决方
时间:2021-01-04
