forked from chuxiong/koa2-File-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
30 lines (23 loc) · 678 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const Koa = require('koa')
const Router = require('koa-router')
const multer = require('koa-multer')
const { join } = require('path')
const app = new Koa
const router = new Router
const storage = multer.diskStorage({
// 存储的位置
destination: join(process.cwd(), "upload"),
// 文件名
filename(req, file, cb){
const filename = file.originalname.split(".")
cb(null, `${Date.now()}.${filename[filename.length - 1]}`)
}
})
const upload = multer({storage})
router.post('/profile', upload.single('file'), async ctx => {
ctx.body = {
filename: ctx.req.file.filename
}
})
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000);