-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (37 loc) · 953 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
37
38
39
40
41
42
43
44
45
46
47
48
const express = require('express');
const mongoose = require('mongoose');
const Blog = require('./models/blog');
// express app
const app = express();
const dbURI = ''
mongoose.connect(dbURI, {useNewUrlParser: true, useUnifiedTopology: true})
.then((result) => app.listen(3000))
.catch((err) => console.log(err));
// register view engine
app.set('view engine', 'ejs');
// middleware & static files
app.use(express.static('public'));
// routes
app.get('/', home);
app.get('/about', about);
app.get('/contact', contact);
app.get('/blogs', allBlogs);
// functions
function home(req, res) {
res.render('index');
};
function about(req, res) {
res.send("hello");
};
function contact(req, res) {
res.send("hello");
};
function allBlogs(req, res) {
Blog.find()
.then((result) => {
res.render('all-blogs', {blogs: result})
})
.catch((err) => {
console.log(err)
});
};