-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
186 lines (161 loc) · 5.29 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require('dotenv').config() //plugin environment variables
// Required to fix CORS errors when making requests
const express = require('express');
const path = require('path'); // path module to work with directories and files
const fs = require('fs'); //files module
const moment = require('moment'); //npm install moment -- for dates and times
const bodyParser = require('body-parser');
const app = express();
const http = require('http').createServer(app); //chat
const io = require('socket.io')(http);//chat
const isProduction = process.env.NODE_ENV === 'production'; // fast feedback and loops
const port = isProduction ? process.env.PORT : 4000; //use port 3000
app.use(express.static(__dirname + '/build')); //middleware for static files (images, css, etc)
app.use(bodyParser.json());
app.get('*', function (req, res, next) {
// Prevents an HTML response for API calls
if (req.path.indexOf('/api/') != -1) {
return next();
}
fs.readFile(__dirname + '/build/index.html', 'utf8', function (err, text) {
res.send(text);
});
});
const cors = require('cors');
const whitelist = [
'http://localhost:8080',
'http://localhost:3000',
'https://tech-education.herokuapp.com/'
];
const corsOptions = {
origin: function(origin, callback) {
const originIsWhitelisted = whitelist.indexOf(origin) !== -1;
callback(null, originIsWhitelisted);
},
credentials: true,
methods: ['GET,PUT,POST,DELETE,OPTIONS'],
allowedHeaders: ['Access-Control-Allow-Headers', 'Origin', 'Access-Control-Allow-Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Cache-Control']
};
app.use(cors(corsOptions));
// Websocket testing (for future use)
app.get('/api/chat/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.on('disconnect', function () {
});
});
io.on('connection', function (socket) {
socket.on('chat message', function (msg) {
});
});
const apiModules = require('./api/modules')
const {
fetchCourses,
fetchCourseById,
fetchCoursesByLanguage,
createCourse,
updateCourse,
deleteCourse,
fetchTopics,
fetchTopicsByLanguage,
fetchTopicsByCourse,
fetchTopicsById,
createTopic,
updateTopic,
deleteTopic,
fetchUsers,
createUser,
updateUser,
deleteUser,
fetchFeedback,
createFeedback,
updateFeedback,
deleteFeedback,
} = apiModules.crud
// Courses
app.get('/api/fetchCourses', (req, res) => {
fetchCourses({}).then(data => res.json(data))
})
app.get('/api/fetchCourse/:id', (req, res) => {
fetchCourseById({ courseId: req.params.id }).then(data => res.json(data))
})
app.get('/api/fetchCoursesByLanguage/:language_id', (req, res) => {
fetchCoursesByLanguage({ languageId: req.params.language_id }).then(data => res.json(data))
})
app.post('/api/courses', (req, res) => {
createCourse({ values: req.body }).then(data => res.json(data))
})
app.patch('/api/courses/:id', (req, res) => {
updateCourse({
courseId: req.params.id,
updateFields: req.body
})
.then(data => res.json(data))
})
app.delete('/api/courses/:id', (req, res) => {
deleteCourse({ courseId: req.params.id }).then(data => res.json(data))
})
// Topics
app.get('/api/fetchTopics', (req, res) => {
fetchTopics({}).then(data => res.json(data))
})
app.get('/api/fetchTopics/:id', (req, res) => {
fetchTopicsById({ topicId: req.params.id }).then(data => res.json(data))
})
app.get('/api/fetchTopicsByCourse/:course_id', (req, res) => {
fetchTopicsByCourse({ courseId: req.params.id }).then(data => res.json(data))
})
app.get('/api/fetchTopicsByLanguage/:language_id', (req, res) => {
fetchTopicsByLanguage({ languageId: req.params.language_id }).then(data => res.json(data))
})
app.post('/api/topics', (req, res) => {
createTopic({ values: req.body }).then(data => res.json(data))
})
app.patch('/api/topics/:id', (req, res) => {
updateTopic({
topicId: req.params.id,
updateFields: req.body
})
.then(data => res.json(data))
})
app.delete('/api/topics/:id', (req, res) => {
deleteTopic({ topicId: req.params.id }).then(data => res.json(data))
})
// Users
app.get('/api/fetchUsers', (req, res) => {
fetchUsers({}).then(data => res.json(data))
})
app.post('/api/users', (req, res) => {
createUser({ values: req.body }).then(data => res.json(data))
})
app.patch('/api/users/:id', (req, res) => {
updateUser({
userId: req.params.id,
updateFields: req.body
})
.then(data => res.json(data))
})
app.delete('/api/users/:id', (req, res) => {
deleteUser({ userId: req.params.id }).then(data => res.json(data))
})
// Feedbacks
app.get('/api/fetchFeedback', (req, res) => {
fetchFeedback({}).then(data => res.json(data))
})
app.post('/api/feedback', (req, res) => {
createFeedback({ values: req.params }).then(data => res.json(data))
})
app.patch('/api/feedback/:id', (req, res) => {
updateFeedback({
feedbackId: req.params.id,
updateFields: req.body
})
.then(data => res.json(data))
})
app.delete('/api/feedback/:id', (req, res) => {
deleteFeedback({ feedbackId: req.params.id }).then(data => res.json(data))
})
app.listen(port, function () {
console.log('TechEducation-ReactWithApi: Server running on port ' + port);
});