// 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
相关推荐
zwjapple1 小时前
docker-compose一键部署全栈项目。springboot后端,react前端像风一样自由20203 小时前
HTML与JavaScript:构建动态交互式Web页面的基石aiprtem4 小时前
基于Flutter的web登录设计浪裡遊4 小时前
React Hooks全面解析:从基础到高级的实用指南why技术4 小时前
Stack Overflow,轰然倒下!GISer_Jing4 小时前
0704-0706上海,又聚上了止观止4 小时前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案whale fall4 小时前
npm install安装的node_modules是什么烛阴5 小时前
简单入门Python装饰器袁煦丞5 小时前
数据库设计神器DrawDB:cpolar内网穿透实验室第595个成功挑战