-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (34 loc) · 1.13 KB
/
index.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
/*
* Title: Markdown Blog
* Description: Simple Markdown blog app using express , mongodb and so on...
* Author: Mohammad Mesbaul Haque
* Date: 18/05/2021
*/
// Dependencies.
const express = require('express');
const methodOverride = require('method-override');
const mongoose = require('mongoose');
const Article = require('./models/Article')
const articleRouter = require('./controllers/articles');
//connect with mongodb
mongoose.connect(process.env.URI, {useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true})
.then(()=>console.log(`connected with atlas`))
.catch(err=>console.log(err))
// Module scaffolding.
const app = express();
app.use(express.urlencoded({extended: false}));
app.use(methodOverride('_method'));
app.set('view engine', 'ejs');
// main functions or objects.
app.get('/', async(req, res)=>{
const articles = await Article.find().sort({createdAt: 'desc'});
res.render('articles/index', {articles: articles})
})
// give permission to use modules
app.use('/articles', articleRouter);
app.listen(process.env.PORT || 3000, ()=>{
console.log(`listening Markdown app`);
})
// export the module.