-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (50 loc) · 1.77 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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
// functions to communicate with drive.js file which communicates with Google Drive API
const drive = require('./drive/drive');
const app = express();
// parse request body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// expose /public folder
app.use(express.static(path.join(__dirname, 'public')));
// serve index.html for the user
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
// user entered folder ID and requested image
// if folder ID is valid send image name, else send error
app.post('/image', (req, res) => {
const folderId = req.body.folderId;
const onDownloadFinished = (name, extension, error = false, errorMsg) => {
if (error) {
res.json({
error: true,
errorMsg: errorMsg
});
} else { // success
res.json({
error: false,
name: name,
extension: extension
});
}
}
drive.download(onDownloadFinished, folderId);
});
// user received image name and is now requesting image file
app.get('/imageFile', (req, res) => {
res.sendFile(path.join(__dirname + `/public/tmp/${req.query.folderId}.${req.query.ext}`));
});
// user clicked on submit button, upload JSON file to Google Drive folder
app.post('/submit', (req, res) => {
const annotationData = JSON.parse(req.body.annotations);
const onSuccess = () => res.sendStatus(200);
drive.upload(onSuccess, req.body.name, annotationData, req.body.folderId);
});
app.listen(process.env.PORT || 3000, () =>
console.log(`Server listening on port ${process.env.PORT || 3000}!`)
);