标签:指定 create mod creat 技术 ons 返回 files png
// server.js const http = require(‘http‘); const fs = require(‘fs‘); http.createServer(function(req,res){ console.log(‘req come‘, req.url); if (req.url === ‘/‘) { const html = fs.readFileSync(‘test.html‘, ‘utf8‘); 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=2000000000, no-cache‘, // 缓存时间很长,但是下次请求时还是需要通过服务端验证 ‘Last-Modified‘: ‘123‘, ‘Etag‘: ‘777‘ }) res.end(‘console.log("script loaded")‘); } }).listen(8888); console.log(‘server listening on 8888‘); console.log(‘http://localhost:8888/‘)
<!--test.html--> <body> <script src=‘/script.js‘></script> </body>
//server.js const http = require(‘http‘); const fs = require(‘fs‘); http.createServer(function(req,res){ console.log(‘req come‘, req.url); if (req.url === ‘/‘) { const html = fs.readFileSync(‘test.html‘, ‘utf8‘); res.writeHead(200,{ ‘Content-Type‘: ‘text/html‘ }) res.end(html); } if (req.url === ‘/script.js‘) { console.log(req.headers); const etag = req.headers[‘if-none-match‘]; if (etag === "777") { res.writeHead(304, { ‘Content-Type‘: ‘text/javascript‘, ‘Cache-Control‘: ‘max-age=2000000000, no-cache‘, // 缓存时间很长,但是下次请求时还是需要通过服务端验证 ‘Last-Modified‘: ‘123‘, ‘Etag‘: ‘777‘ }) res.end(‘‘); } else { res.writeHead(200,{ ‘Content-Type‘: ‘text/javascript‘, ‘Cache-Control‘: ‘max-age=2000000000, no-cache‘, // 缓存时间很长,但是下次请求时还是需要通过服务端验证 ‘Last-Modified‘: ‘123‘, ‘Etag‘: ‘777‘ }) res.end(‘console.log("script loaded")‘); } } }).listen(8888); console.log(‘server listening on 8888‘); console.log(‘http://localhost:8888/‘);
标签:指定 create mod creat 技术 ons 返回 files png
原文地址:https://www.cnblogs.com/wzndkj/p/10074742.html