-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathserver.js
60 lines (51 loc) · 1.96 KB
/
server.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
const express = require("express");
//bring in mongoose
const mongoose = require("mongoose");
//bring in method-override
const methodOverride = require("method-override");
const blogRoute = require("./routes/blogs");
const Blog = require("./models/Blog");
const dotenv = require('dotenv');
dotenv.config();
const app = express();
app.use('/public', express.static('public'))
// lets get connected with Mongoose
mongoose.connect(
process.env.CONNECTION_URL,
//process.env.MONGO_URL is the environment variable that is set in the .env file
{
useNewUrlParser: true,
//useNewUrlParser is a method that is used to parse the url
useUnifiedTopology: true,
//useUnifiedTopology is a method that is used to connect to the database
useCreateIndex: true,
//useCreateIndex is a method that is used to create indexes
},
(err) => {
console.log(err || "Connected to MongoDB");
}
);
// Lets set template engine
app.set("view engine", "ejs");
// set() is a method that is used to set a value for a key
app.use(express.urlencoded({ extended: false }));
// use() is a method that is used to use a middleware
// express.urlencoded() is a middleware that is used to parse the data sent by the user
app.use(methodOverride("_method"));
// methodOverride() is a middleware that is used to override the method
// Route for index
app.get("/", async (req, res) => {
const blogs = await Blog.find().sort({ timeCreated: "desc" });
// find() => this method finds and returns all documents that match the query criteria.
// sort() => this method sorts the documents in the collection.
res.render("index", { blogs: blogs });
});
app.use(express.static("public"));
// use() is a method that is used to use a middleware
// express.static() is a middleware that is used to serve static files
app.use("/blogs", blogRoute);
var port = process.env.PORT || 5000;
app.listen(port, (err) => {
if (err) throw err;
console.log(`Server listening on port http://localhost:${port}`);
});