forked from BhathiyaPrasad/Nodejs-Backend-Development-Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (67 loc) · 1.7 KB
/
app.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const express = require('express');
const { method, result } = require('lodash');
const morgan = require('morgan');
const mongoose = require('mongoose');
const { render } = require('ejs');
const { request } = require('http');
const blogRoutes = require('./routes/blogRouter');
// express app
const app = express();
// connect to the mongodb database
const dbURI = 'mongodb+srv://bhathiya:[email protected]/node-tuts?retryWrites=true&w=majority&appName=nodetuts';
mongoose.connect(dbURI)
.then((result) => app.listen(3000))
.catch((err) => console.log(err))
// listen for requests
//middleware static files
app.use(express.static('public'));
app.use(express.urlencoded({extended: true}));
app.use(morgan('dev'));
// mongoose and mongo sandbox routes
app.get('/add-blog', (req, res) => {
const blog = new Blog({
title: 'New Blog',
snippet: 'About My New V',
body: 'more About my new Blog'
});
blog.save()
.then((result) => {
res.send(result)
})
.catch((err) => {
console.log(err);
})
});
app.get('/all-blog',(req,res) => {
Blog.find()
.then((result) => {
res.send(result);
})
.catch(err => {
console.log(err);
});
});
app.get('/single-blog',(req, res) => {
Blog.findById('65f3db4928d7dee04cf4c972')
.then((result) => {
res.send(result)
})
.catch(err =>{
console.log(err);
});
});
// register view engine
app.set('view engine', 'ejs');
// app.set('views', 'myviews');
app.get('/', (req, res) => {
res.redirect('/blogs');
});
app.get('/about', (req, res) => {
res.render('about', { title: 'About' });
});
// blog routes
app.use('/blogs', blogRoutes);
// 404 page
app.use((req, res) => {
res.status(404).render('404', { title: '404' });
});