-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
60 lines (41 loc) · 1.59 KB
/
server.mjs
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
import express from "express";
import bodyParser from 'body-parser';
import twilio_route from './routes/twilio_respond.js'
import delete_history_route from './routes/delete_history.js'
import send_initial_text_route from './routes/send_initial_text.js'
import default_route from './routes/default.js'
const port = process.env.PORT || 3000;
// Set up app
const server_app = express();
server_app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
// Set up body parser for all routes
server_app.use(bodyParser.urlencoded({ extended: false }));
// DEFAULT ROUTE
server_app.use('/', default_route);
// TWILIO ROUTE
server_app.use('/sms-receiver-responded', twilio_route);
// DELETE HISTORY
server_app.use('/delete-history', delete_history_route);
// SEND INITIAL TEXT
server_app.use('/send-initial-text', send_initial_text_route);
// error 404
server_app.use((req, res, next) => { res.status(404).send('404: Not Found \"' + req.path + '\"');});
// This function is used to bind the server to a specific port and start listening for incoming connections.
server_app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
// development error handler
// will print stacktrace
if (server_app.get('env') === 'development') {
server_app.use(function(err, req, res) {
res.status(err.status || 500).send(err.message || 'Internal Server Error');
});
}
// production error handler
// no stacktraces leaked to user
server_app.use(function(err, req, res) {
res.status(err.status || 500).send('Internal Server Error');
});