-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (32 loc) · 1021 Bytes
/
index.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
import express from "express";
import { v5 as uuidv5 } from "uuid";
import multer from "multer";
import fs from "fs";
import path from "path";
const publicFilesPath = process.cwd() + "/public";
const imageFilesPath = process.cwd() + "/images";
const app = express();
app.use(express.static(publicFilesPath));
app.get("/", (req, res) => {
res.sendFile(publicFilesPath + "/index.html");
});
app.get("/image/:id", (req, res) => {
const id = req.params.id;
console.log(id);
res.sendFile(imageFilesPath + "/" + id);
});
const upload = multer({
limits: {
fileSize: 4 * 1024 * 1024
},
dest: "/tempImg"
})
app.post("/upload", upload.single("image"), (req, res) => {
const ogName = req.file.originalname;
const newImgName = req.file.filename + path.extname(ogName);
fs.rename(req.file.path, imageFilesPath + "/" + newImgName, console.log);
res.redirect("/image/" + newImgName);
});
app.listen(process.env.port || 3000, () => {
console.log("Service started");
});