-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
113 lines (92 loc) · 3.38 KB
/
app.js
File metadata and controls
113 lines (92 loc) · 3.38 KB
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
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var validator = require('express-validator');
var hbs = require('hbs');
//session-based packages
var session = require('express-session');
var MongoStore = require('connect-mongo')(session); //uses MongoDB to store session data
//application start-up
var app = express();
//defining different route handlers
var root = require('./routes/root');
var sendData = require('./routes/sendData');
var getData = require('./routes/getData');
var profile = require('./routes/profile');
var register = require('./routes/register');
var login = require('./routes/login');
var logout = require('./routes/logout');
//middleware for logging new requests info and verifying that a user is logged in
var checkLogin = require('./libs/requireLogin');
var logReqInfo = require('./libs/logRequestInfo');
//importing a secret used for cookies and other stuff
var secretString = require('./libs/secret');
//establishing database connection
mongoose.connect('mongodb://localhost:27017/learn', function (err) {
if (err) console.log('Can \'t establish connection with the database');
else console.log('Successfully connected to MongoDB');
});
//setting up a default view engine and folder
//which contains different views
app.set('views', './views');
app.set('view engine', 'hbs');
//parsing data from all requests into JSON format
app.use(bodyParser.json());
//URL encoding needed for form data
app.use(bodyParser.urlencoded({ extended: false }));
//used for sanitizing user input
app.use(validator());
app.use(function (req, res, next) {
for (var item in req.body) {
req.sanitize(item).escape();
}
next();
});
/*
//creating a new session
app.use(session({
secret: secretString,
resave: false, //don't save session again if it hasn't been modified
saveUninitialized: false, //don't save sessions that haven't yet gotten any info, e.g. user hasn't yet logged in
store: new MongoStore({ url: 'mongodb://localhost:27017/learn' })
}));
*/
//checking user's JWT token and logging info about req object
app.use(checkLogin);
app.use(logReqInfo);
//mounting handler to the root path in order to
//respond with the homepage
app.use('/', root);
//mouting handler to the /register path in order for a user
//to be able to register
app.use('/register', register);
//mounting handler to the /login path in order for a user
//to be able to login
app.use('/login', login);
//mounting handler to the /logout path in order for a user
//to be able to logout
app.use('/logout', logout);
//mouting handler to the /data path in order to
//send back data from the database to a user
app.use('/data', getData);
//mouting handler to the /send path in order to
//get data from a user which is located in the request body
app.use('/send', sendData);
//mouting handler to the /profile path in order to
//send user's account data to the user for review
app.use('/profile', profile);
//defining default 404 response
app.use(function (req, res) {
res.status(404).send('Error 404 didn\'t find that!');
});
//default 500 response
app.use(function (err, req, res, next) {
console.log(err.stack);
res.type('text/plain');
res.status(500);
res.send('500 - Server Error');
});
app.listen(8081, function (err) {
if (err) console.log('Error starting the server');
else console.log('Server has started on port 8081');
});