-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (73 loc) · 1.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dbURI =
"mongodb+srv://netninja:[email protected]/node-tuts?retryWrites=true&w=majority";
const Blog = require("./models/blog");
const blogRoutes = require("./routes/blogRoutes");
mongoose
.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then((result) => {
console.log("connected to database");
app.listen(3000);
})
.catch((err) => {
console.log("neuspjesno konektovanje na bazu");
console.log(err);
});
var morgan = require("morgan");
app.set("view engine", "ejs");
app.set("views", "views");
//middleware and static files
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(morgan("dev"));
// app.get("/add-blog", (req, res) => {
// const blog = new Blog({
// title: "Naslov",
// snippet: "Snippet of a new blog",
// body: "The body of a new blog",
// });
// blog
// .save()
// .then((result) => {
// res.send(result);
// })
// .catch((err) => {
// console.log(err);
// });
// });
// app.get("/all-blogs", (req, res) => {
// Blog.find()
// .then((results) => {
// res.send(results);
// })
// .catch((err) => {
// console.log(err);
// });
// });
// app.get("/single-blog", (req, res) => {
// Blog.findById("5f61de32a81c870ae835f600")
// .then((result) => {
// res.send(result);
// })
// .catch((err) => {
// console.log(err);
// });
// });
app.get("/", (req, res) => {
res.redirect("/blogs");
});
app.get("/about", (req, res) => {
// res.sendFile("./views/about.html", { root: __dirname });
res.render("about", { title: "About", blogs: "blogs" });
});
//redirect
app.get("/about-us", (req, res) => {
res.redirect("/create_blogs");
});
app.use(blogRoutes);
//404
app.use((req, res) => {
res.status(404).render("404", { title: "404" });
});