Node实现四种缓存

  const http = require('http')

// Expires
let server = http.createServer((req, res) => {
  res.setHeader('Expires', new Date().toGMTString())
  res.end('harttle.land')
})


// Cache-Control
let server = http.createServer((req, res) => {
  res.setHeader('Cache-Control', 'public, max-age=86400')
  res.end('harttle.land')
})

// Etag
let server = http.createServer((req, res) => {
  console.log(req.url, req.headers['if-none-match'])
  if(req.headers['if-none-match']) {
    res.statusCode = 304
    res.end()
  }else {
    res.setHeader('Etag', '12345678')
    res.end('harttle.land')
  }
})

// Last-Modified
let server = http.createServer((req, res) => {
  console.log(req.url, req.headers['if-modified-since'])
  if(req.headers['if-modified-since']){
    res.statusCode = 304
    res.end()
  }else {
    res.setHeader('Last-Modified', new Date().toISOString())
    res.end('harttle.land')
  }
})

console.log('server start at http://localhost:3333')
server.listen(3333)

你可能感兴趣的:(Node实现四种缓存)