-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
32 lines (27 loc) · 1.05 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
const express = require('express')
const mongoose = require('mongoose')
const Article = require('./models/article')
const articleRouter = require('./routes/articles')
const methodOverride = require('method-override')
const {configDotenv} = require("dotenv")
configDotenv();
const app = express()
const port = process.env.PORT || 3000;
const marked = require('marked');
const DB = `mongodb+srv://Adnan:${process.env.DB_PASSWORD}@markdown-blogs-cluster.ctjoxbh.mongodb.net/BlogsDB?retryWrites=true&w=majority&appName=Markdown-Blogs-cluster`;
mongoose.connect(DB).then(() => {
console.log('Connected to MongoDB');
}).catch((error) => {
console.error('Error connecting to MongoDB:', error);
});
app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended: false }))
app.use(methodOverride('_method'))
app.get('/', async (req, res) => {
const articles = await Article.find().sort({ createdAt: 'desc' })
res.render('articles/index', { articles: articles })
})
app.use('/articles', articleRouter)
app.listen(port, ()=>{
console.log(`Listening on port ${port}`)
});