源码及应用阐明 Git仓库
装置
npm install koa-static-router
Usage
- 单个路由
const static = require('koa-static-router'); app.use( static({ dir, //动态资源目录对于绝对入口文件index.js的门路 route //路由命名 }) )
- 多个路由
抉择多个路由时,请确保路由长度雷同
'/static/' - >路由长度 = 1
'/static/image1/' - >路由长度 =2
const static = require('koa-static-router');app.use( static([ { dir, //动态资源目录对于绝对入口文件index.js的门路 router //路由命名 },{ dir, router } ]))
Demo
git clonecd koa-static-routernpm install npm start
- 拜访 localhost:3000/public/image/dir/1.png
- 拜访 localhost:3000/static/image/dir/2.png
const Koa = require('koa')const app = new Koa()const static = require('koa-static-router');// 单个路由// app.use(static({// dir:'public',// router:'/static/' //路由长度 =1// }))//多个路由app.use(static([ { dir:'public', //动态资源目录对于绝对入口文件index.js的门路 router:'/public/image/' //路由命名 路由长度 =2 },{ dir:'static', //动态资源目录对于绝对入口文件index.js的门路 router:'/static/image/' //路由命名 路由长度 =2 }]))app.use( async ( ctx ) => { ctx.body = 'hello world'})app.listen(3000, () => { console.log('build success')})