-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·46 lines (38 loc) · 1.36 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
const express = require('express');
const mongoose = require('mongoose');
const fileUpload = require('express-fileupload');
const methodOverride = require('method-override');
const postController = require('./controllers/postController');
const pageController = require('./controllers/pageController')
const app = express();
// CONNECT TO DB
mongoose.connect('mongodb+srv://cleanBlog:[email protected]/myFirstDatabase?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology: true
});
//MIDDLEWARE
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(fileUpload({}));
app.use(
methodOverride('_method', {
methods: ['POST', 'GET'],
})
);
//TEMPLATE ENGINE
app.set('view engine', 'ejs');
//ROUTES
app.get('/', postController.getAllPosts);
app.get('/posts/:id', postController.getPostByID);
app.post('/posts', postController.createPost);
app.put('/posts/:id', postController.updatePost);
app.delete('/posts/:id', postController.deletePost);
app.get('/about', pageController.getAboutPage);
app.get('/add_post', pageController.getAddPage);
app.get('/posts', pageController.getPosts);
app.get('/posts/update/:id', pageController.getUpdatePage);
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});