-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (59 loc) · 1.82 KB
/
app.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 fs = require("fs");
const express = require("express");
const multer = require("multer");
const path = require("path");
const rootPath = path.dirname(require.main.filename);
process.env.GOOGLE_APPLICATION_CREDENTIALS = `${rootPath}/g_client.json`;
const app = express();
const port = process.env.PORT || 3000;
const gVision = require("./public/js/gVision");
// SETUP APP
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
// MAIN CONSTANT FOR UPLOAD IMAGE
const upload = multer({
storage: multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'public/upload/');
},
filename: (req, file, cb) => {
const ext = file.mimetype.split('/')[1];
cb(null, file.originalname);
}
}),
fileFilter: (req, file, cb) => {
if (file.mimetype.startsWith('image')) {
console.log("photo uploaded");
cb(null, true);
} else {
console.log("file not supported");
cb(null, false);
}
}
}).single('photo');
/* ROUTES */
app.get("/", async(req, res, next) => {
res.status(200).render("index.html");
});
app.post("/upload", upload, async(req, res, next) => {
const text = await gVision(req.file.path);
// fs.unlink(req.file.path, err => {
// if (err) return console.log(err);
// console.log('photo deleted');
// });
res.status(200).json({
text,
filename: req.file.filename
});
});
app.post("/delete", async (req, res, next) => {
await fs.unlink(`public/${req.query.filename}`, err => {
if (err) return console.log(err);
console.log('photo deleted');
});
res.status(200).json({
status: 'success'
});
});
// RUN SERVER
app.listen(port, () => console.log(`Server listening on port ${port}`));