-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
45 lines (36 loc) · 1 KB
/
main.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
// imports
require('dotenv').config()
const express = require('express')
const mongoose = require('mongoose')
const session = require('express-session')
const app = express()
const PORT = process.env.PORT || 4000
// database connection
mongoose.connect(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
const db = mongoose.connection
db.on('error', (error) => console.log(error))
db.once('open', () => console.log('connected to the database'))
// middlewares
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use(session({
secret: 'my secret key',
saveUninitialized: true,
resave: false,
}))
app.use((req, res, next) => {
res.locals.message = req.session.message
delete req.session.message
next()
})
app.use(express.static('uploads'))
// set template engine
app.set('view engine', 'ejs')
// route prefix
app.use('/', require('./routes/routes'))
app.listen(PORT, () => {
console.log(`Server started at http://localhost:${PORT}`)
})