-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (49 loc) · 1.62 KB
/
app.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
var express = require('express');
var exphbs = require('express-handlebars');
var flash = require('connect-flash');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var Joi = require('joi');
var path = require('path');
var app = express();
var indexRoute = require('./routes/index');
var booksRoutes = require('./routes/books');
var genresRoutes = require('./routes/genres');
var usersRoutes = require('./routes/users');
// body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended :false}));
app.use(cookieParser());
// public path
app.use(express.static(path.join(__dirname, 'public')));
// view engine
app.set("views", path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
// connect flash
app.use(flash());
// Global variable
app.use(function (req, res, next) {
res.locals.testVar = "tasfin";
//res.locals.success_msg = req.flash('success_msg');
//res.locals.error_msg = req.flash('error_msg');
//res.locals.error = req.flash('error');
next();
})
mongoose.connect('mongodb://localhost/nodeapp', { useNewUrlParser: true });
var db = mongoose.connection;
// set routes
app.use('/', indexRoute);
app.use('/', booksRoutes);
app.use('/', genresRoutes);
app.use('/', usersRoutes);
// set port
app.set('port', (process.env.PORT || 3000));
if(!module.parent){
app.listen(app.get('port'), function () {
console.log('Server started on port '+app.get('port')+'...');
});
}
module.exports = app;