-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathapp.js
More file actions
58 lines (48 loc) · 1.48 KB
/
Copy pathapp.js
File metadata and controls
58 lines (48 loc) · 1.48 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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const logger = require("morgan");
const sentiment = require("sentiment");
const accents = require("remove-accents");
// Loads collection of last words
const lastWords = require("./lastWords");
var port = process.env.PORT || 8080;
app.use(logger("dev"));
app.use(bodyParser.json());
// HOME ROUTE
app.get("/", (req, res) => {
var searchResults = lastWords;
searchResults.forEach((person) => {
if (person.translation) {
person.sentiment = sentimentAnalyzer(person.translation);
} else {
person.sentiment = sentimentAnalyzer(person.lastWords);
}
});
res.send(searchResults);
});
// GET QUOTE BY PERSON'S LAST NAME
app.get("/lastname/:lastname", (req, res) => {
var searchResults = lastWords.filter((person) => {
return accents.remove(person.lastName).toLowerCase().includes(accents.remove(req.params.lastname).toLowerCase());
});
searchResults.forEach((person) => {
if (person.translation) {
person.sentiment = sentimentAnalyzer(person.translation);
} else {
person.sentiment = sentimentAnalyzer(person.lastWords);
}
});
if (searchResults.length > 0) {
res.send(searchResults);
} else {
res.send('No matching results were found.');
}
// Return error
// Redirect if no match is found
});
// ANALYZES SENTIMENT OF LAST WORDS
const sentimentAnalyzer = words => sentiment(words);
app.listen(port, () => {
console.log(`SERVER IS RUNNING ON PORT ${port}`);
});