-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
50 lines (41 loc) · 1.07 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
require("dotenv").config();
const express = require("express");
const morgan = require("morgan");
const mongoose = require("mongoose");
const blogRoutes = require("./routes/blogRoutes");
// express app
const app = express();
//connect to db
mongoose
.connect(process.env.DB_URL)
.then((result) => console.log("connected db"))
.catch((err) => console.log(err));
// listen for requests
// app.listen(3000 || )
// register view engine
app.set("view engine", "ejs");
// middleware & static files
app.use(express.static("public"));
app.use(morgan("dev"));
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
res.locals.path = req.path;
next();
});
//Basic Routes
app.get("/", (req, res) => {
res.redirect("/blogs");
});
app.get("/about", (req, res) => {
res.render("about", { title: "About" });
});
//Blog Routes
app.use("/blogs", blogRoutes);
// 404 page
app.use((req, res) => {
res.status(404).render("404", { title: "404" });
});
const PORT = process.env.PORT || 80;
app.listen(PORT, () => {
console.log("app is running at port", PORT);
});