美文网首页KOA
8KOA 静态文件

8KOA 静态文件

作者: 帶頭二哥 | 来源:发表于2020-01-06 02:05 被阅读0次

静态文件

使用 koa-static 中间件实现静态文件访问

安装中间件
npm install koa-static --save
使用中间件
// 引入模块
const StaticService = require('koa-static')

// 创建静态服务,并且设置指向路径
const staticMiddleware = StaticService('./public')

app.use(staticMiddleware)

使用 koa-mount 自定义静态服务路径

介绍

用于指定路径访问中间件,与 router 中间件类似

安装中间件
npm install koa-mount --save
使用中间件
// 引入模块
const StaticService = require('koa-static')
// 引入 mount 模块
const mount = require('koa-mount')

// 创建静态服务,并且设置指向路径
const publicStaticService = StaticService('./public')
app.use(mount('/public',publicStaticService))

const uploadStaticService = StaticService('./upload')
app.use(mount('/upload',uploadStaticService))

相关文章

网友评论

    本文标题:8KOA 静态文件

    本文链接:https://www.haomeiwen.com/subject/bzrhactx.html