-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
52 lines (42 loc) · 1.47 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
const express = require("express");
const mongoose = require("mongoose");
const path = require("path");
const ejs = require("ejs");
const post = require("./models/Post");
const Post = require("./models/Post");
const methodOverride = require("method-override");
const postController = require('./controllers/postController')
const pageController = require('./controllers/pageController')
const app = express();
//Database Connect
mongoose.connect("mongodb+srv://aesweb:[email protected]/cleanblog-db?retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
console.log('DB CONNECTED')
}).catch((err) => {
console.log(err);
})
//Template Engine
app.set("view engine", "ejs");
//MIDDLEWARES
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(methodOverride("_method", {
methods:['POST', 'GET']
}));
// ROUTES
app.get("/", postController.getAllPosts);
app.get("/posts/:id", postController.getPost);
app.post("/posts", postController.createPost);
app.get("/posts/edit/:id", postController.updatePost);
app.delete("/posts/:id", postController.deletePost);
app.get("/about", pageController.getAboutPage);
app.get("/post", pageController.getPostPage);
app.get("/add_post", pageController.getAddPage);
app.put("/posts/:id", pageController.getEditPage);
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server started on ${port} port`);
});