-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
31 lines (26 loc) · 999 Bytes
/
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
const express = require("express");
const articleRouter = require("./routes/articles");
const Article = require("./models/article");
const mongoose = require("mongoose");
const methodOverride = require("method-override");
const dotenv = require("dotenv").config();
const port = process.env.PORT || 3000;
const app = express();
const username = process.env.MONGODB_USERNAME;
const password = process.env.MONGODB_PASSWORD;
const databaseName = "blogDB";
// connect to database
mongoose.connect(
`mongodb+srv://${username}:${password}@cluster0.cklpl3g.mongodb.net/${databaseName}`
);
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: false }));
app.use(methodOverride("_method"));
app.get("/", async (req, res) => {
const articles = await Article.find().sort({ createdAt: "desc" });
res.render("articles/index", { articles: articles });
});
app.use("/articles", articleRouter);
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});