koa2系列教程,持續更新
這篇主要介紹koa2處理靜態文件的中間件
用到的版本:
項目的結構:
大家可以往static文件夾裏面添加點東西
編輯index.js
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
有福利哦