-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (37 loc) · 1.42 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
// Import modules and functions from thrid-party application and local files
const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose')
const { render } = require('express/lib/response');
const blogRoutes = require('./routes/blogRoutes')
// Intialize an express app
const app = express();
// Connect to the Mongodb database
const dbuRI = 'mongodb+srv://john:<password>@nodetuts.vfcpj.mongodb.net/node-tuts?retryWrites=true&w=majority';
mongoose.connect(dbuRI, {useNewUrlParser: true, useUnifiedTopology: true})
// Start the localhost
.then((result)=> app.listen(5000))
// Check for any error with localhost startup
.catch((err) => console.log(err));
// Using Dev from the morgan middleware to log the time for requests
app.use(morgan('dev'));
// Registering an Ejs template engine with Express
app.set('view engine', 'ejs');
// Using any static files located in the public folder (styles.css)
app.use(express.static('public'));
// Middleware Files
app.use(express.urlencoded({extended: true}));
app.use((req, res, next) => {
res.locals.path = req.path;
next();
});
// Use the Blog Routes script when the home page is accessed
app.use('/blogs', blogRoutes);
// When trying to access "localhost:5000/" redirect to "localhost:5000/blogs"
app.get('/', (req, res) => {
res.redirect('/blogs');
});
// 404 page
app.use((req, res) => {
res.status(404).render('404', { title: '404' });
});