-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
55 lines (49 loc) · 1.65 KB
/
app.js
File metadata and controls
55 lines (49 loc) · 1.65 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
// Loading Modules
const express = require('express');
const hbs = require('express-handlebars');
const bodyParser = require('body-parser');
const config = require('./config.json');
const app = express();
const admin = require('./routes/admin');
const path = require('path');
const mongoose = require('mongoose');
const session = require('express-session');
const flash = require('connect-flash');
// Configuration
// Session
app.use(session({
secret: config.sessionSecret,
resave: true,
saveUninitialized: true
}));
app.use(flash())
// Middleware
app.use((req, res, next) => {
res.locals.success_msg = req.flash("success_msg")
res.locals.error_msg = req.flash("error_msg")
next()
})
// BodyParser
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json())
// HandleBars (HBS)
app.engine('hbs', hbs({defaultLayout: 'main', extname: "hbs", helpers: {appName: config.appName}}));
app.set('view engine', 'hbs')
// Mongoose (DataBase)
mongoose.connect(config.MongoDB, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(()=>{
console.log("Successfully connected to MongoDB");
}).catch(err=>{
console.error("Error connecting to MongoDB")
})
// Public (css, js, images)
app.use(express.static(path.join(__dirname,"public")))
// Routes
// Route admin
app.use('/admin', admin);
// Others
app.listen(config.port, () => {
console.log("The server was started on the port " + config.port);
})