slackkoala 2019-06-29
koa2系列教程,持续更新
这篇主要介绍koa2处理静态文件的中间件
用到的版本:

项目的结构:

大家可以往static文件夹里面添加点东西
const Koa = require('koa')
const statics = require('koa-static')
const path = require('path')
const app = new Koa()
const staticPath = './static'
app.use(statics(
path.join(__dirname, staticPath)
))
app.use(async (ctx) => {
ctx.body = 'hello world'
})
app.listen(3000)
console.log('localhost:3000')node index.js
然后在浏览器访问:http://localhost:3000/images/node.png
可以看到:

或者访问其他静态文件,都可以访问了
源码地址:https://github.com/xiaqijian/koa2-lessons/tree/master/lesson2
首发于微信公众号:node前端
不妨关注一下,我们一起学习
回复:100
有福利哦
