-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
66 lines (57 loc) · 1.62 KB
/
server.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
62
63
64
65
66
const express = require("express");
const multer = require("multer");
const path = require("path");
const cors = require("cors");
const app = express();
app.use(express.static(path.join(__dirname, "client", "build")));
app.use(cors());
//Storage Engine
const storage = multer.diskStorage({
destination: "./client/public/uploads/",
filename: function (req, file, cb) {
cb(
null,
file.fieldname + "-" + Date.now() + path.extname(file.originalname)
);
},
});
const upload = multer({
storage: storage,
limits: { fileSize: 1000000 },
fileFilter: function (req, file, cb) {
imageCheck(file, cb);
},
}).single("image");
//image check
function imageCheck(file, cb) {
const filetypes = /gif|jpg|jpeg|png/;
//extension check
const extname = filetypes.test(
path.extname(file.originalname).toLowerCase()
);
//mime check
const mimetype = filetypes.test(file.mimetype);
if (extname && mimetype) {
return cb(null, true);
} else {
cb(null, false);
return cb("Only images are allowed");
}
}
app.post("/upload", (req, res) => {
upload(req, res, err => {
if (req.files === null) {
return res.status(400).json({ msg: "No file uploaded" });
}
if (err) {
return res.status(500).json({ msg: err });
} else {
res.json({
fileName: req.file.filename,
filePath: `/uploads/${req.file.filename}`,
});
}
});
});
const port = 5000;
app.listen(5000, () => console.log(`server started on port ${port}`));