-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabcde.js
More file actions
80 lines (65 loc) · 1.83 KB
/
Copy pathabcde.js
File metadata and controls
80 lines (65 loc) · 1.83 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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const config = require('./config/config');
const session = require('express-session');
const index = require('./routes/index');
const users = require('./routes/users');
const app = express();
const router = express.Router();
mongoose.connect(config.database);
mongoose.Promise = global.Promise;
mongoose.connection.once('open', () => {
console.log('MongoDB connection has been made!');
}).on('error', (error) => {
console.log('Connection error: ', error);
});
// Middlewares
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(session({
secret: 'ioasudhs',
saveUninitialized: true,
resave: true
}));
// local variables
app.use((req, res, next) => {
res.locals.userLoggedIn = req.session.user;
res.locals.flashMsg = null;
next();
});
// set view engine to ejs
app.set('views', path.join(__dirname, 'app', 'views'));
app.set('view engine', 'ejs');
// set static files path
app.use(express.static(path.join(__dirname, 'public')))
// make sure the param id is valid
router.param('id', (req, res, next, id) => {
if (!id.match(/^[a-f\d]{24}$/i)) {
let err = new Error('Invalid ID!');
err.status = 400;
return res.render('error', {error: err});
}
next();
});
index(router);
users(router);
app.use(router);
// error handling
app.get('*', (req, res, next) => {
let error = new Error('Page not found!');
error.status = 404;
res.render('error', {
error: error
});
});
app.use((err, req, res, next) => {
res.render('error', {
error: err
});
});
app.listen(config.port, () => {
console.log(`Server is listening to requests on port ${config.port}`)
});
module.exports = app;