koa2系列教程:koa2處理靜態文件

koa2系列教程,持續更新


這篇主要介紹koa2處理靜態文件的中間件

用到的版本:

image.png

項目的結構:

image.png

大家可以往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

可以看到:

image.png

或者訪問其他靜態文件,都可以訪問了

源碼地址:https://github.com/xiaqijian/koa2-lessons/tree/master/lesson2


首發於微信公衆號:node前端

不妨關注一下,我們一起學習

回覆:100

有福利哦

image.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章