-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
66 lines (54 loc) · 1.35 KB
/
Copy pathapp.js
File metadata and controls
66 lines (54 loc) · 1.35 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const pathLib = require("path");
const app = express();
app.listen(8080);
// 引入Multer
const multer = require("multer");
// 设置保存上传文件路径
const upload = multer({
dest: "./static/upload",
});
// 处理上传文件
app.use(upload.any());
// 处理表单提交,对应请求头application/x-www-form-urlencoded
app.use(
bodyParser.urlencoded({
extended: false, // 为true时将使用qs库处理数据,通常不需要
})
);
// 处理fetch请求,对应请求头application/json
app.use(bodyParser.json());
// 接收文件上传结果
app.post("/upload", (req, res, next) => {
console.log(req.files);
//上传的文件在files里
const newName =
req.files[0].path + pathLib.parse(req.files[0].originalname).ext;
fs.rename(req.files[0].path, newName, function (err) {
if (err) {
res.send(err);
} else {
res.send("succeed");
}
});
});
app.get("/reg", (req, res, next) => {
console.log(req.query);
res.send({
error: 0,
data: req.query,
msg: "注册成功",
});
});
app.post("/login", (req, res, next) => {
console.log(req.body);
res.send({
error: 0,
data: req.body,
msg: "登录成功",
});
});
app.use(express.static("./static/"));
console.log(`app started at 8080`);