-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (38 loc) · 1.21 KB
/
index.js
File metadata and controls
47 lines (38 loc) · 1.21 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
const http = require("http");
const path = require("path");
const fs = require("fs");
const express = require("express");
const app = express();
const httpServer = http.createServer(app);
const muster1 = require("multer");
const PORT = 3000;
httpServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
const handleError = (err, res) => {
res
.status(500)
.contentType("text/plain")
.end("Oops! Something went wrong!");
};
const upload = muster1({
dest: "Your Path"
// ex) /Users/WebstormProjects/express-s3-upload/uploads
});
app.get("/", express.static(path.join(__dirname, "./public")));
app.post("/upload", upload.single("file"), (req, res) => {
console.log(req)
const tempPath = req.file.path;
const filename = './uploads/' + req.file.originalname
const targetPath = path.join(__dirname, filename);
fs.rename(tempPath, targetPath, err => {
if (err) return handleError(err, res);
res.send(req.file.originalname);
});
}
);
app.get("/:media", (req, res) => {
const {media} = req.params;
const filename = "./uploads/" + media
res.sendFile(path.join(__dirname, filename));
});