-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomserver.js
More file actions
48 lines (38 loc) · 1.26 KB
/
Copy pathcustomserver.js
File metadata and controls
48 lines (38 loc) · 1.26 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
// require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const path = require("path");
const fs = require("fs");
const { connectToDatabase } = require("./db");
const routes = require("./routes");
const customserver = express();
const port = process.env.PORT || 3900;
customserver.use(bodyParser.json());
customserver.use(cors());
(async () => {
await connectToDatabase();
const writableRoot = process.env.USER_DATA_PATH || __dirname;
const UPLOAD_DIR = path.join(writableRoot, "uploads");
// console.log("Static File Server Root:", UPLOAD_DIR);
if (!fs.existsSync(UPLOAD_DIR)) {
try {
fs.mkdirSync(UPLOAD_DIR, { recursive: true });
} catch (err) {
console.error("Could not create upload dir:", err);
}
}
customserver.use("/uploads", express.static(UPLOAD_DIR));
customserver.use("/api", routes);
customserver.get("/", (req, res) => {
res.status(200).send(true);
});
customserver.listen(port, () => {
console.log(`Server is running on port ${port}`);
// console.log(`Serving uploads from: ${UPLOAD_DIR}`);
});
})();
customserver.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Something broke!");
});