惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
A
About on SuperTechFans
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
H
Help Net Security
月光博客
月光博客
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
T
Tailwind CSS Blog
Jina AI
Jina AI
有赞技术团队
有赞技术团队
博客园_首页

姓王者的博客

Linux用户Secure Boot自主维护指南 | 姓王者的博客 MAD Bugs 已经开始——关于信息安全的军备竞赛 | 姓王者的博客 解决钉钉Dingtalk无法在Linux新版内核上启动问题-修复可执行栈错误 | 姓王者的博客 突发:GitHub 正遭受大规模 Issue 赌博广告轰炸 | 姓王者的博客 Ubuntu26.04-beta体验:坚毅浣熊! | 姓王者的博客 fakeclaw装作龙虾发贴吧 | 姓王者的博客 找回12年前的QQ记忆 | 姓王者的博客 在Linux上玩Flash网页游戏-洛克王国 | 姓王者的博客 Copilot将使用交互数据来训练 | 姓王者的博客 重要通知-请更新我的GPG公钥 | 姓王者的博客 为了自由Android | 姓王者的博客 GPL"2,3"事 | 姓王者的博客 短文-对VitePlus的一点🤏小贡献 | 姓王者的博客 Bing收录没了?亲测有效的快速恢复指南 | 姓王者的博客 解决桌面设备二维码快速识别的工具-ClipQR | 姓王者的博客 解决 Nautilus 自定义终端插件安装依赖问题 | 姓王者的博客 OpenClaw 该熄火了 | 姓王者的博客 Vite8 - 统一的基建开始 | 姓王者的博客 Astro 6 推出啦 | 姓王者的博客 ubuntu的openvpn异常暂停推送更新 | 姓王者的博客 Ubuntu 24.04 安装 Win10 虚拟机 | 姓王者的博客 ESA-后记:热爱阿里云 | 姓王者的博客 Moonbit 0.8.0 重大发布,我也要改一下我的包 | 姓王者的博客 ESA Pages 边缘开发大赛获奖 | 姓王者的博客 Astro: 优化katex,mermaid和灯箱使用 | 姓王者的博客 从edgeone迁移到esa | 姓王者的博客 出租人类:AI时代的荒诞与真实 | 姓王者的博客 Astro 5.17构建性能优化实践:从18s到13s | 姓王者的博客 Moonbit License Checker 开发使用 | 姓王者的博客 Stalux Astro博客主题自荐 | 姓王者的博客
node.js:简单的HTTP服务器 | 姓王者的博客
作者:xingwangzhe · 2024-12-11 · via 姓王者的博客

🕒 阅读时间:1 分钟 📝 字数:326 👀 阅读量: Loading...

有意思,实现文件访问了

引入需要的部分

const http = require("http");

const fs = require("fs");

const path = require("path");

第一个http服务,第二个fs文件操作,第三个路径相关

第一步:创建http

const server = http.createServer((req, res) => {});

第二部:http回调函数

定义路径

在回调函数里面写功能 首先定义路径

let url = new URL(req.url, `http://${req.headers.host}`);

let pathname = url.pathname;

let filePath = path.join(__dirname, pathname);

//可以加个log看一下路径是否正确,这会在服务器运行,路径访问时打印

// console.log('Request for:' , filePath);

读取文件

fs.readFile(filePath, (err, data) => {

if (err) {

res.writeHead(404, { "Content-Type": "text/plain" });

res.end("Not found");

return;

} else {

// 根据文件扩展名设置Content-Type

// const ext = path.extname(filePath).toLowerCase();

//const mimeTypes = {

//'.html': 'text/html',

//'.js': 'application/javascript',

//'.css': 'text/css',

// 其他文件类型...

//现代浏览器的资源嗅探已经足够先进,因此对文件扩展名设置Content-Type并不是必要的

//};

// const contentType = mimeTypes[ext] || 'application/octet-stream';

// res.writeHead(200, {'Content-Type': contentType});

res.end(data);

}

});

设置端口,启动服务器

server.listen(3000, () => {

console.log("Server running at http://localhost:3000/");

});

完整代码

const server = http.createServer((req, res) => {

// 使用req.url和req.headers.host来创建完整的URL对象

let url = new URL(req.url, `http://${req.headers.host}`);

let pathname = url.pathname;

// 构建文件系统路径

let filePath = path.join(__dirname, pathname);

console.log("Request for:", filePath);

fs.readFile(filePath, (err, data) => {

if (err) {

res.writeHead(404, { "Content-Type": "text/plain" });

res.end("Not found");

return;

} else {

// // 根据文件扩展名设置Content-Type

// const ext = path.extname(filePath).toLowerCase();

// const mimeTypes = {

// '.html': 'text/html',

// '.js': 'application/javascript',

// '.css': 'text/css',

// // 其他文件类型...

// 现代浏览器的资源嗅探已经足够先进,因此对文件扩展名设置Content-Type并不是必要的

// };

// const contentType = mimeTypes[ext] || 'application/octet-stream';

// res.writeHead(200, {'Content-Type': contentType});

res.end(data);

}

});

});

server.listen(3000, () => {

console.log("Server running at http://localhost:3000/");

});

hello world

2024-12-11-175244