-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (24 loc) · 886 Bytes
/
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
const express = require('express');
const dotenv = require('dotenv').config();
const connectDB = require('./config/db');
const articleRouter = require('./routes/article');
const Article = require('./models/article');
const methodOverride = require('method-override')
// Init db
connectDB()
// express app
const app = express();
// view engine convert all the ejs code to html
app.set('view engine', 'ejs');
// to access req.body.title etc
app.use(express.urlencoded({ extended: false }));
// to use delete method in html
app.use(methodOverride('_method'));
app.get('/', async (req, res) => {
articles = await Article.find().sort({ createdAt: 'desc' })
res.render('articles/index', { articles: articles });
});
app.use('/articles', articleRouter)
// PORT
const port = process.env.PORT || 3000
app.listen(port, () => console.log(`server is listening at ${port}`));