-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (50 loc) · 1.55 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
56
57
58
59
60
61
62
63
64
65
66
const express = require('express')
const mongoose = require('mongoose')
const Blog = require('./model/blogSchema')
const router = require('./routes/blogroutes')
const authRouter = require('./routes/authRoutes')
const cookieParser = require('cookie-parser')
const { checkUser } = require('./middlewares/auth')
require('dotenv').config();
const app = express();
//use Middleware
app.use(express.static('public'))
app.use('/upload/', express.static('upload'))
app.use(express.urlencoded({ extended: true }))
app.use(express.json()) //This is use to parse only json data!
app.use(cookieParser())
//Register view engine
app.set('view engine', 'ejs')
// port
const port = process.env.PORT || 5000
// db connection
const dbURI = process.env.DB_CONNECTION
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, autoIndex: true })
.then(result => {
console.log('Connected to the Database!...')
app.listen(port, '0.0.0.0', () => {
console.log(`Server is live on port ${port}`)
})
})
.catch(err => {
console.log(err)
})
//===========Routes=================
app.get('*', checkUser)
app.get('/', async (req, res) => {
try {
const blogs = await Blog.find().sort({createdAt: -1})
res.render('index', { blogs })
}
catch (err) {
console.log(err)
}
})
app.get('/about', (req, res) => {
res.render('about')
})
app.use(router)
app.use(authRouter)
/* app.listen(port, '0.0.0.0', () => {
console.log(`Server is live on port ${port}`)
}) */