-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
61 lines (51 loc) · 1.67 KB
/
api.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import dayjs from "dayjs";
import { db_insert, db_find, db_remove } from "./db.js";
import path from "path";
import { fileURLToPath } from "url";
import express from "express";
import { call_py } from "./pyfunc.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const api_config = (app, db) => {
app.get("/", async (req, res) => {
let ctx = req.body;
req.session.uid = "x";
let now = dayjs().format();
ctx.now = now;
console.log(now);
await call_py("/", { test: 1 });
res.json(ctx);
});
// for vue project inside
// app.use("/", express.static(path.join(__dirname, "frontend/dist")));
app.post("/", async (req, res) => {
let ctx = req.body;
res.json(ctx);
});
app.post("/upload", function (req, res) {
// <form
// ref="uploadForm"
// id="uploadForm"
// action="http://localhost:8000/upload"
// method="post"
// encType="multipart/form-data"
// >
// <input type="file" name="sampleFile" />
// <input type="submit" value="Upload!" />
// </form>;
let sampleFile;
let uploadPath;
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send("No files were uploaded.");
}
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
sampleFile = req.files.sampleFile;
uploadPath = __dirname + "/upload/" + sampleFile.name;
// Use the mv() method to place the file somewhere on your server
sampleFile.mv(uploadPath, function (err) {
if (err) return res.status(500).send(err);
res.send("File uploaded!");
});
});
return app;
};