-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
78 lines (63 loc) · 1.9 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
67
68
69
70
71
72
73
74
75
76
77
78
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const dotenv = require("dotenv");
dotenv.config();
const path = require("path");
const port = process.env.PORT || 5000;
const AppsScript = require("./appsscript.js");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Static file declaration
app.use(express.static(path.join(__dirname, "client/build")));
//production mode
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", (req, res) => {
res.sendfile(path.join((__dirname = "client/build/index.html")));
});
}
//build mode
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/client/public/index.html"));
});
// Post request to upload excel file to google drive and sync data to firebase
app.post("/signup/:id", (req, res) => {
const fileurl = req.body.file;
try {
var appsscript = new AppsScript(fileurl);
var callAppsScript = async () => {
var excelId = await appsscript.runAppsScript();
return excelId;
};
callAppsScript()
.then((excelId) => {
res.send(excelId);
})
.catch((err) => res.status(404).send("Error"));
} catch (err) {
res.status(400).send("Error");
}
});
// Post request to update firebase with new excel file
app.post("/edit-account/:id", (req, res) => {
const fileurl = req.body.file;
try {
var appsscript = new AppsScript(fileurl);
var callAppsScript = async () => {
var excelId = await appsscript.updateFirebase();
return excelId;
};
callAppsScript()
.then((excelId) => {
res.send(excelId);
})
.catch((err) => res.status(404).send("Error"));
} catch (err) {
res.status(404).send("Error");
}
});
//start server
app.listen(port, (req, res) => {
console.log(`server listening on port: ${port}`);
});