server3.js
const http = require('http'); const fs = require('fs'); http.createServer(function(req, res) { console.log('request come', req.url); const html = fs.readFileSync('index.html', 'utf8'); if (req.url === '/') { res.writeHead(200, { "Content-Type":"text/html" }) res.end(html) } if (req.url === '/script.js') { res.writeHead(200, { "Content-Type": "text/javascript", "Cache-Control":"max-age=20" }) res.end('console.log("script loaded")') } }).listen(8888) console.log('server start at port 8888')
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> 8888 <script src="/script.js"></script> </body> </html>
server3.js
修改緩存最大時間,和打印內容
const http = require('http'); const fs = require('fs'); http.createServer(function(req, res) { console.log('request come', req.url); const html = fs.readFileSync('index.html', 'utf8'); if (req.url === '/') { res.writeHead(200, { "Content-Type":"text/html" }) res.end(html) } if (req.url === '/script.js') { res.writeHead(200, { "Content-Type": "text/javascript", "Cache-Control":"max-age=200" }) res.end('console.log("script loaded 222")') } }).listen(8888) console.log('server start at port 8888')
在200秒內請求,仍是會返回原來的內容javascript
超出200秒,會返回新的內容html