-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (80 loc) · 2.78 KB
/
Copy pathserver.js
File metadata and controls
95 lines (80 loc) · 2.78 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
////////////////////////////////////////////////////////////
//
// File: server.js
// Developer: Arthur Dibe
//
// HOW TO START:
// 1) npm install
// 2) npm start
//
// OBS: Once you run the server, it will generate a file
// called "output.txt" where you can check some outputs
// examples when callling the routes
//
////////////////////////////////////////////////////////////
const express = require("express");
const fetch = require("node-fetch");
const generateOutputs = require("./saveOutputs.js");
const helper = require('./helperFunctions.js')
const chalk = require('chalk');
const yellow = chalk.bold.yellow
const magenta = chalk.magenta
const green = chalk.bold.green
const app = express();
// to parse the incoming requests
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const HTTP_PORT = process.env.PORT || 3000;
app.listen(HTTP_PORT, () => {
console.log(`[${yellow(`SERVER ${green('ON')}`)}]: ${magenta(HTTP_PORT)}`);
console.log("--------------------------------------------");
});
// Generate outputs
generateOutputs()
// ROUTE 1
app.get("/api/ping", (req, res) => {
res.status(200).json({ success: true });
});
// ROUTE 2
app.get("/api/posts", async (req, res) => {
const tags = req.query.tags;
const sortBy = req.query.sortBy || "id";
const direction = req.query.direction || "asc";
const sortOptions = ["id", "reads", "likes", "popularity"];
const directOptions = ["asc", "desc"];
let codeStatus = 400;
let respObj;
let responseAPI;
if (!tags) {
respObj = { error: "Tags parameter is required" };
} else if (!sortOptions.find((option) => option == sortBy)) {
respObj = { error: "sortBy parameter is invalid" };
} else if (!directOptions.find((option) => option == direction)) {
respObj = { error: "direction parameter is invalid" };
} else {
let responses = [];
const tagList = tags.split(",");
// ------------- Fetch the posts from API - synchronize by using Promise.all and .map
await Promise.all(
tagList.map(async (tag) => {
const url = `https://api.hatchways.io/assessment/blog/posts?tag=${tag}`;
const options = {
method: "GET",
};
responseAPI = await fetch(url, options).then((resp1) => resp1.json());
responses.push(...responseAPI.posts);
})
);
// ------------- Sort the Elements
responses = helper.sortElements(responses, sortOptions, sortBy, direction)
// ------------- Remove Repeated Objects
const uniqueResponse = helper.removeRepeatedElements(responses)
codeStatus = 200;
respObj = { posts: uniqueResponse };
}
res.status(codeStatus).json(respObj);
});
// ROUTE 404 - route not found
app.use((req, res) => {
res.status(404).json({ error: "Request failed with status code 404" });
});