-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (52 loc) · 1.91 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
const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');
const blogRoutes = require('./routes/blogRoutes');
//express app
const app =express();
//mongo connection
const dburi = 'mongodb+srv://user1:[email protected]/nodeCrash?retryWrites=true&w=majority';
mongoose.connect(dburi, { useNewUrlParser: true,useUnifiedTopology: true } )
.then((result) => {console.log('Connected to DB');
app.listen(3000);})
.catch((err) => console.log(err));
// register view engine
//app.set('views', 'myviews'); // if the default views folder is named differently
app.set('view engine', 'ejs');
// listen for request now inside db connection
//app.listen(3000);
// middleware & static
app.use(express.static('public'));
app.use(morgan('dev'));
app.use(express.urlencoded({extended: true}));
app.get('/', (req,res) => {
//res.send('<p> Home Page </p>');
//res.sendFile('./views/index.html', { root: __dirname })
// const blogs = [
// {title: 'Yoshi finds eggs', snippet: 'Lorem ipsum dolor sit amet consectetur'},
// {title: 'Mario finds stars', snippet: 'Lorem ipsum dolor sit amet consectetur'},
// {title: 'How to defeat bowser', snippet: 'Lorem ipsum dolor sit amet consectetur'},
// ];
// res.render('index', { title: 'Home',blogs});
res.redirect('/blogs');
});
app.get('/about', (req,res) => {
//res.send('<p> about Page </p>');
//res.sendFile('./views/about.html', { root: __dirname })
res.render('about',{ title: 'About'});
});
app.get('/blogs/create', (req,res) => {
res.render('create', { title: 'Create'});
});
// redirects
app.get('/about-us', (req,res) => {
//res.send('<p> about Page </p>');
res.redirect('/about');
});
//blog routes
app.use('/blogs',blogRoutes);
//app.use(blogRoutes);
// 404 page
app.use((req,res) => {
res.status(404).render('404', { title: 'Page not found'});
});