使用koa-static访问静态资源
下载:
npm install --save koa-static
1.引入koa-static中间件,用于访问静态文件
const Koa = require('koa')
// 引入node的path模块
const path = require('path')
// koa-static中间件,用于访问静态文件
const static = require('koa-static')
const app = new Koa()
2.使用中间件
// 使用中间件,利用path模块的方法拼接出静态文件目录的绝对路径
app.use(static(path.join(__dirname,staticPath)))
3.启动服务,即可访问静态资源
app.listen(3000,()=>{
console.log('server starting at loaclhost:3000')
})