静态资源
静态资源和动态资源
动态资源:通过服务器动态生成的资源,如数据库里的数据、根据动态数据展示的页面;
静态资源:不需要动态计算的,如图片、html、css等;
静态资源服务器
可以访问静态资源的服务器(好像一句废话)
实现访问图片的static server
fs.readFile('./cmdb.png','binary',function(err, file) {if (err) {console.log(err)return}else{http.createServer(function(req, res) {res.writeHead(200, {'Content-Type': 'image/jpeg'})res.write(file,'binary')res.end()return}).listen(8888)}});
express实现静态服务器
// 将dist文件夹下的内容作为静态服务器,默认页面为index.html,const express = require('express')const path = require('path')const app = express()app.use(express.static(path.join(__dirname, '../dist')))app.listen(5000)
