node强缓存

复制代码
// 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}`)
})
相关推荐
J***Q2921 小时前
Vue数据可视化
前端·vue.js·信息可视化
ttod_qzstudio3 小时前
深入理解 Vue 3 的 h 函数:构建动态 UI 的利器
前端·vue.js
_大龄3 小时前
前端解析excel
前端·excel
一叶茶4 小时前
移动端平板打开的三种模式。
前端·javascript
前端大卫4 小时前
一文搞懂 Webpack 分包:async、initial 与 all 的区别【附源码】
前端
Want5954 小时前
HTML音乐圣诞树
前端·html
老前端的功夫4 小时前
前端浏览器缓存深度解析:从网络请求到极致性能优化
前端·javascript·网络·缓存·性能优化
Running_slave5 小时前
你应该了解的TCP滑窗
前端·网络协议·tcp/ip
程序员小寒5 小时前
前端高频面试题之CSS篇(一)
前端·css·面试·css3
颜酱6 小时前
Monorepo 架构以及工具选型、搭建
前端·javascript·node.js