-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·166 lines (151 loc) · 4.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import React from "react";
import App from "./src/App.js";
import { renderToString } from "react-dom/server";
import StyleContext from "isomorphic-style-loader/StyleContext";
import { SheetsRegistry } from "jss";
import JssProvider from "react-jss/lib/JssProvider";
import {
MuiThemeProvider,
createMuiTheme,
createGenerateClassName
} from "@material-ui/core/styles";
import green from "@material-ui/core/colors/green";
import red from "@material-ui/core/colors/red";
import yellow from "@material-ui/core/colors/yellow";
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
const methodOverride = require("method-override");
const http = require("http");
const cron = require("node-cron");
const TaskDB = require("./database/database.js");
const schedulerFactory = require("./schedulerFactory.js");
const port = process.env.PORT || 3000;
const moment = require('moment');
const test = new Date();
// console.log(test);
// console.log(moment.utc(test).format());
// const MessagingResponse = require('twilio').twiml.MessagingResponse;
app.use(methodOverride("_method"));
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
// to support URL-encoded bodies
extended: true
})
);
app.get("/", function(req, res) {
//grabs from DB
// console.log('this is a GET request')
let tasks;
TaskDB.find({})
.exec()
.then(data => {
tasks = data;
// console.log("THIS IS TASKS********",tasks);
})
.then(() => {
const css = new Set(); // CSS for all rendered React components
const insertCss = (...styles) =>
styles.forEach(style => css.add(style._getCss()));
// Create a sheetsRegistry instance.
const sheetsRegistry = new SheetsRegistry();
// Create a sheetsManager instance.
const sheetsManager = new Map();
// Create a theme instance.
const theme = createMuiTheme({
palette: {
primary: green,
secondary: yellow,
type: "light"
}
});
// Create a new class name generator.
const generateClassName = createGenerateClassName();
const appString = renderToString(
<JssProvider
registry={sheetsRegistry}
generateClassName={generateClassName}
>
<MuiThemeProvider theme={theme} sheetsManager={sheetsManager}>
<StyleContext.Provider value={{ insertCss }}>
<App tasks={tasks} />
</StyleContext.Provider>
</MuiThemeProvider>
</JssProvider>
);
const css1 = sheetsRegistry.toString();
// console.log("****css****\n", css, "\n");
// console.log('****CSS1****\n', css1)
const indexFile = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<style>${[...css].join("")}${css1}</style>
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root">${appString}</div>
</body>
</html>
`;
res.status(200).send(indexFile);
})
.catch(err => {
console.log(err, "THIS IS A GET PROMISE ERROR");
});
});
app.post("/tasks", function(req, res) {
//database endpoint; submits data from user input
// let tasks;
// console.log(res.body, "RES.BODY");
console.log(req.body, "REQ.BODY");
// req.body = {
// task : 'to dooo',
// time: {date},
// timezone: 'EST'
// }
// '2016-01-01T23:35:01' 2019-04-25 20:51:00.000
// CONVERT THEIR TIME from their TIMEZONE to Pacific TIME
// CREATE DOCUMENT OBJECT WITH THE NEW CONVERTED TIME AND THEN INSERT TO DATABASE
TaskDB.create(req.body, (err, result) => {
if (err) {
console.log("ERROR IN CREATE ", err);
}
console.log("CREATE SUCCESS ", result);
});
res.status(200).redirect("/");
// })
// .catch(err => {
// console.log(err, "THIS IS A POST PROMISE ERROR");
// });
// });
});
app.delete("/tasks/:id", function(req, res) {
// console.log("DELETE REQUEST WORKING");
// console.log(req.params);
// let tasks;
TaskDB.findByIdAndRemove({ _id: req.params.id }, function(err, data) {
if (err) {
console.log("DELETE REQUEST ERROR");
}
});
res.status(200).redirect("/");
// })
// .catch(err => {
// console.log(err, "THIS IS A DELETE PROMISE ERROR");
// });
// });
});
schedulerFactory.start();
app.listen(port);
console.log("Node server running on port ",port);
// module.exports = schedulerFactory();