Skip to content

Commit 0994afa

Browse files
authored
Merge pull request #67 from jonah-butler/url-params-parts-of-speech
2 parents 16a86ad + 6815a70 commit 0994afa

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@
1111

1212
- PWA Demo - [Check Here](https://words.sanweb.info/)
1313

14+
## 🎛 Route Options
15+
16+
- Base URL: `https://random-words-api.vercel.app/word`
17+
```html
18+
- /noun
19+
- /sentence
20+
- /question
21+
- /adjective
22+
- /idiom
23+
- /verb
24+
- /letter
25+
- /paragraph
26+
- /vocabulary
27+
- /1-word-quotes
28+
- /2-word-quotes
29+
- /3-word-quotes
30+
- /affirmation
31+
```
32+
1433
## 🌐 Sample API Response
1534

1635
```json

routes/en.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { pronounce } = require("node-pronounce");
66
const randomUseragent = require("random-useragent");
77
const rua = randomUseragent.getRandom();
88
var wordOfDay = [];
9+
const baseUrl = 'https://randomword.com';
910

1011
router.get("/", function (req, res) {
1112
res.header("Access-Control-Allow-Origin", "*");
@@ -22,7 +23,73 @@ router.get("/", function (req, res) {
2223

2324
axios({
2425
method: "GET",
25-
url: "https://randomword.com/",
26+
url: baseUrl,
27+
headers: {
28+
"User-Agent": rua,
29+
},
30+
})
31+
.then(function (response) {
32+
$ = cheerio.load(response.data);
33+
if (wordOfDay.length > 0) {
34+
wordOfDay = [];
35+
}
36+
37+
var post = $(".section #shared_section");
38+
var word = post
39+
.find("#random_word")
40+
.eq(0)
41+
.text()
42+
.replace("\r\n\t\t\t\t\t", "")
43+
.replace("\r\n\t\t\t\t", "")
44+
.replace("\n\t\t\t\t\t", "")
45+
.replace("\n\t\t\t\t", "");
46+
var definition = post
47+
.find("#random_word_definition")
48+
.eq(0)
49+
.text()
50+
.replace("\n", "");
51+
var pronounceword = pronounce(word).replace(",", "");
52+
53+
wordOfDay.push({
54+
word: decodeURI(word.charAt(0).toUpperCase() + word.slice(1)),
55+
definition: decodeURI(
56+
definition.charAt(0).toUpperCase() + definition.slice(1)
57+
),
58+
pronunciation: decodeURI(
59+
pronounceword.charAt(0).toUpperCase() + pronounceword.slice(1)
60+
),
61+
});
62+
res.send(JSON.stringify(wordOfDay, null, 2));
63+
})
64+
.catch(function (error) {
65+
if (!error.response) {
66+
console.log("API URL is Missing");
67+
res.json("API URL is Missing");
68+
} else {
69+
console.log("Something Went Wrong - Enter the Correct API URL");
70+
res.json("Something Went Wrong - Enter the Correct API URL");
71+
}
72+
});
73+
});
74+
75+
router.get("/:pos", function (req, res) {
76+
res.header("Access-Control-Allow-Origin", "*");
77+
res.header(
78+
"Access-Control-Allow-Headers",
79+
"Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With"
80+
);
81+
res.header("Access-Control-Allow-Methods", "GET");
82+
res.header("X-Frame-Options", "DENY");
83+
res.header("X-XSS-Protection", "1; mode=block");
84+
res.header("X-Content-Type-Options", "nosniff");
85+
res.header("Strict-Transport-Security", "max-age=63072000");
86+
res.setHeader("Content-Type", "application/json");
87+
88+
const partOfSpeech = req.params.pos;
89+
90+
axios({
91+
method: "GET",
92+
url: `${baseUrl}/${partOfSpeech}`,
2693
headers: {
2794
"User-Agent": rua,
2895
},

0 commit comments

Comments
 (0)