-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
36 lines (28 loc) · 1.16 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
const express = require('express');
const mongoose = require('mongoose');
const postRoutes = require('./routes/postRoutes');
const methodOverride = require('method-override');
// Create a new express application instance
const app = express();
// Cinnect to the database
const dbURI = 'mongodb+srv://express:express@blog-with-node.swwvqfw.mongodb.net/blog-with-node?retryWrites=true&w=majority'
mongoose.connect(dbURI, {useNewUrlParser: true, useUnifiedTopology: true})
.then(() => app.listen(3000, () => console.log('listening on port 3000 ... http://localhost:3000')))
.catch(err => console.log(err));
// Configure view engine to render EJS templates
app.set('view engine', 'ejs');
// Middleware && static files
app.use(express.static('public'));
app.use(express.urlencoded({extended: true}));
app.use((req, res, next) => {
res.locals.path = req.path;
next();
});
app.use(methodOverride('_method'));
// routes
app.get('/', (req, res) => res.redirect('/posts'));
app.get('/about', (req, res) => res.render('about', {title: "About"}));
// Blog routes
app.use('/posts',postRoutes);
// 404 page
app.use((req, res) => res.status(404).render('404', {title: "404"}));