-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (48 loc) · 1.37 KB
/
server.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
const express = require("express")
const path =require("path")
const homeRoute=require('./route/homeRoute')
const app = express();
var bodyParser = require('body-parser')
const expressValidator = require('express-validator')
const flash=require('connect-flash')
const session = require('express-session')
const port = 3000
//setup mongoose
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/articleBD', { useNewUrlParser: true, useUnifiedTopology: true })
let db =mongoose.connection;
//check the DB connection
db.once('open',()=>{
console.log("Connected to DB")
})
db.on('error',(err)=>{
console.log(err)
})
//load view engine
app.set('views',path.join(__dirname,'views'))
// https://www.npmjs.com/package/pug
app.set('view engine', 'pug')
app.use(express.static('public'))
// express session
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true,
}))
// express messages
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
// Express Validator Middleware
app.use(expressValidator())
//setup bodyParser
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
// homePage Route
homeRoute(app)
app.listen(port, () => {
console.log(` listening at http://localhost:${port}`)
})