-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
57 lines (43 loc) · 1.39 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
const express = require("express");
const mongoose = require("mongoose");
const router = require("./routes/blogRoutes")
// creating an express app
const app = express();
// connect to mongo db
const dbURI = 'mongodb+srv://Leoravoe_Kel:preetamsarkar@12@nodetutorial.fndob.mongodb.net/Node-tuts?retryWrites=true&w=majority'
mongoose.connect(dbURI, {useNewUrlParser : true,useUnifiedTopology: true})
.then((result) => {
console.log("connected to the database")
// listening to particular port only after the db is connected to
app.listen(3000);
})
.catch((err) =>{
// console.log("I entered")
console.log(err)
})
// register view engine
app.set("view engine", "ejs");
//middleware & static files
app.use(express.static("public"))
app.use(express.urlencoded({extended: true}));
// basic routing particular
app.get("/", (req, res) => {
//res.send('<p> hi hello</p>');
// res.sendFile("./index.html", { root: __dirname });
// res.render("index",{title: 'Home'});
res.redirect("/blogs")
});
app.get("/about", (req, res) => {
// res.sendFile("./about.html", { root: __dirname });
res.render("about",{title: 'About'});
});
app.get("/create", (req, res) => {
// console.log("I entered")
res.render("create",{title: 'Create a blog'});
});
// blog routes
app.use('/blogs',router)
// 404 error handling
app.use((req, res) => {
res.status(404).render("404",{title: 'Error'});
});