// 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
相关推荐
moxiaoran57531 小时前
uni-app萌宠案例学习笔记--页面布局和CSS样式设置CrissChan2 小时前
Pycharm 函数注释小小小小宇2 小时前
Vue.nextTick()笔记小约翰仓鼠4 小时前
vue3子组件获取并修改父组件的值Lin Hsüeh-ch'in4 小时前
Vue 学习路线图(从零到实战)烛阴4 小时前
bignumber.js深度解析:驾驭任意精度计算的终极武器计蒙不吃鱼4 小时前
一篇文章实现Android图片拼接并保存至相册全职计算机毕业设计5 小时前
基于Java Web的校园失物招领平台设计与实现啊~哈5 小时前
vue3+elementplus表格表头加图标及文字提示