koa-static中间件使用

使用例子

demo源码

https://github.com/ChenShenhai/koa2-note/blob/master/demo/static-use-middleware/

  1. const Koa = require('koa')
  2. const path = require('path')
  3. const static = require('koa-static')
  4. const app = new Koa()
  5. // 静态资源目录对于相对入口文件index.js的路径
  6. const staticPath = './static'
  7. app.use(static(
  8. path.join( __dirname, staticPath)
  9. ))
  10. app.use( async ( ctx ) => {
  11. ctx.body = 'hello world'
  12. })
  13. app.listen(3000, () => {
  14. console.log('[demo] static-use-middleware is starting at port 3000')
  15. })

效果

访问http://localhost:3000

static-server-result

访问http://localhost:3000/index.html

static-server-result

访问http://localhost:3000/js/index.js

static-server-result