// index.js
const http = require('http')
const fs = require('fs')
const path = require('path')
const server = http.createServer((req, res) => {
let filePath = path.resolve(__dirname, req.url === '/' ? `index.html` : '1.jpg')
res.writeHead(200, {
'Content-Type': req.url === '/' ? 'text/html; charset=utf-8' : 'image/png',
'Cache-Control': 'max-age=86400', // 设置缓存缓存一天
})
const fileStream = fs.createReadStream(filePath)
return fileStream.pipe(res)
})
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n')
})
server.listen(8080, () => {
console.log(`opened server on http://localhost:${server.address().port}`)
})
node强缓存
猛男敲代码2024-09-14 17:04
相关推荐
你的人类朋友5 分钟前
什么是API签名?会豪2 小时前
Electron-Vite (一)快速构建桌面应用中微子2 小时前
React 执行阶段与渲染机制详解(基于 React 18+ 官方文档)唐某人丶2 小时前
教你如何用 JS 实现 Agent 系统(2)—— 开发 ReAct 版本的“深度搜索”中微子2 小时前
深入剖析 useState产生的 setState的完整执行流程遂心_3 小时前
JavaScript 函数参数传递机制:一道经典面试题解析小徐_23333 小时前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!RoyLin3 小时前
TypeScript设计模式:适配器模式遂心_3 小时前
深入理解 React Hook:useEffect 完全指南Moonbit3 小时前
MoonBit 正式加入 WebAssembly Component Model 官方文档 !