Git repository
The installation
npm install koa-static-router
Copy the code
Usage
- A single routing
const static = require('koa-static-router');
app.use(
static({
dir, // Static resource directory for relative entry file index.js path
route // Route names}))Copy the code
- Multiple routing
When multiple routes are selected, ensure that the route length is the same. ‘/static/’ – > Route length = 1 ‘/static/image1/’ – > Route length =2
const static = require('koa-static-router');
app.use(
static([
{
dir, // Static resource directory for relative entry file index.js path
router // Route names
},{
dir,
router
}
])
)
Copy the code
Demo
git clone
cd koa-static-router
npm install
npm start
Copy the code
- Access localhost: 3000 / public/images/dir / 1. PNG
- Access localhost: 3000 / static/image/dir / 2. PNG
const Koa = require('koa')
const app = new Koa()
const static = require('koa-static-router');
// Single route
// app.use(static({
// dir:'public',
// router:'/static/' // Route length =1
// }))
// Multiple routes
app.use(static([{dir:'public'.// Static resource directory for relative entry file index.js path
router:'/public/image/' // Route Named Route Length =2}, {dir:'static'.// Static resource directory for relative entry file index.js path
router:'/static/image/' // Route Named Route Length =2
}
]))
app.use( async ( ctx ) => {
ctx.body = 'hello world'
})
app.listen(3000.() = > {
console.log('build success')})Copy the code