-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo15.js
40 lines (34 loc) · 1.11 KB
/
demo15.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
30
31
32
33
34
35
36
37
38
39
40
// 异步上传图片实现
const Koa = require("koa");
const Views = require("koa-views");
const path = require("path");
const Static = require("koa-static");
const { uploadFile } = require("./util/upload-v2");
const app = new Koa();
const port = 5200;
// 使用第三方中间件 start
app.use(Views(path.join(__dirname, "./view"), { extension: "ejs" }));
app.use(Static(path.join(__dirname, "./static")));
// 使用第三方中间件 end
app.use(async (ctx) => {
if (ctx.method === "GET") {
const title = "异步传图";
await ctx.render("upload", { title });
} else if (ctx.url === "/api/picture/upload.json" && ctx.method === "POST") {
// 上传文件请求处理
let result = { success: false };
const serverFilePath = path.join(__dirname, "static/image");
// 上传文件事件
result = await uploadFile(ctx, {
fileType: "album",
path: serverFilePath,
});
ctx.body = result;
} else {
// 其他请求显示404
ctx.body = "<h1>404!!! o(╯□╰)o</h1>";
}
});
app.listen(port, () => {
console.log("访问地址为: http://localhost:%s", port);
});