forked from AvocadoPower/shush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (66 loc) · 1.99 KB
/
index.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
/**
* Server index file to define servable routes
* route-specific functions inside of lib/requestHelper.js
*/
require('dotenv').config();
const express = require('express');
const session = require('express-session');
const request = require('request');
const bodyParser = require('body-parser');
const util = require('./lib/requestHelper');
const app = express();
const cookieParser = require('cookie-parser');
const morgan = require('morgan');
const { User } = require('./db/config');
const PORT = process.env.PORT || 3000;
app.use(express.static(`${__dirname}/dist`));
// set morgan to log info about our requests for development
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
// handle /user route
app.get('/user', util.getUsers);
app.post('/user', util.addUser);
app.use(
session({
key: 'user_sid',
secret: 'somerandonstuffs',
resave: false,
saveUninitialized: false,
cookie: {
expires: 600000
}
})
);
app.use((req, res, next) => {
if (req.cookies.user_sid && !req.session.user) {
res.clearCookie('user_sid');
}
next();
});
// route for user signup
app.post('/signup', util.addUser);
// route for user Login
app.post('/login', util.authenticateUser);
// route for user logout
app.get('/logout', (req, res) => {
if (req.session.user && req.cookies.user_sid) {
res.clearCookie('user_sid');
res.send(200, 'successful logout!');
} else {
res.send(401, 'user not logged in!');
}
});
// handle /trigger route
app.get('/trigger', util.getUserTriggers);
app.post('/trigger', util.addTrigger);
app.put('/trigger', util.updateTrigger);
app.delete('/trigger', util.deleteTrigger);
// route for handling 404 requests(unavailable routes)
app.use(function (req, res, next) {
res.status(404).send('Sorry can\'t find that!');
});
app.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
});