-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
23 lines (22 loc) · 822 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
require("pug")
const express = require("express")
const favicon = require('express-favicon')
const { fetchNews } = require("./src/news")
const PORT = process.env.PORT || 8080
const app = express()
app
.set("view engine", "pug")
.use(favicon(__dirname + "/public/favicon.ico"))
.get("/", async (req, res) => {
try {
const startTime = Date.now()
const page = req.query.page || 1;
const news = await fetchNews(page);
const duration = Date.now() - startTime;
console.log(`Found ${news ? news.length : 0} news articles in ${duration}ms`);
res.render("index", { news, page, duration })
} catch (error) {
console.error(error)
res.status(500).json({ ok: false, error: "Failed to get the news" })
}
}).listen(PORT, () => console.log(`Server Started on ${PORT}`))