-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (24 loc) · 818 Bytes
/
index.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
const express = require("express");
const path = require("path");
require("dotenv").config();
const connectDB = require("./config/db");
const urlRoutes = require("./routes/urlRoutes");
const Url = require("./models/url");
const app = express();
const PORT = process.env.PORT || 8000;
connectDB();
app.set("view engine", "ejs");
app.set("views", path.resolve("views"));
app.use(express.static(path.join(__dirname, "views/public")));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/url/", urlRoutes);
app.get("/", async (req, res) => {
try {
const allUrls = await Url.find({});
res.render("index.ejs", { urls: allUrls });
} catch (err) {
res.render("index.ejs", { urls: [] });
}
});
app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));