-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (39 loc) · 1.48 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
const express = require('express');
const path = require('path');
const exphbs = require('express-handlebars');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 4000;
// Connecting to the database
mongoose
.connect('mongodb://localhost:27017/handlebars-cms')
.then(() => console.log('MongoDB Connect...'))
.catch(error => console.log('Error: ', error));
// Getting the main routes from the home routes folder to use
const home = require('./routes/home/index');
const admin = require('./routes/admin/index');
const posts = require('./routes/admin/posts');
// Express middleware for serving static files
app.use(express.static(path.join(__dirname, 'public')));
// Setting up the express engine to use handlebars as the view
app.engine('handlebars', exphbs({defaultLayout: 'home'}));
// Setting up the express middleware for setting the view engine
app.set('view engine', 'handlebars');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));
// parse application/json
app.use(bodyParser.json());
// Using the middelware for the main routes
app.use('/', home);
// Using the middelware for the admin routes
app.use('/admin', admin);
// Using the middleware for the post routes
app.use('/admin/posts', posts);
// Set the app to listen on a specific port
app.listen(port, err => {
if (err) {
throw err;
}
console.log(`Application is running on port: ${port}`);
});