Skip to content

Commit 586084f

Browse files
committed
refactor(routes): ♻️ Change GET to POST in Pronounce API Route
1 parent 404ce76 commit 586084f

File tree

3 files changed

+57
-34
lines changed

3 files changed

+57
-34
lines changed

README.md

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Random Words API
2+
23
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
4+
35
[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors-)
6+
47
<!-- ALL-CONTRIBUTORS-BADGE:END -->
58

69
![build-test](https://github.com/mcnaveen/Random-Words-API/workflows/build-test/badge.svg)
@@ -11,27 +14,18 @@
1114

1215
- Free API - `https://random-words-api.vercel.app/word`
1316
- Dutch Random Words - `https://random-words-api.vercel.app/word/dutch`
14-
- Get Pronunciation for a Word - `https://random-words-api.vercel.app/pronounce/hello%20world`
17+
- Get Pronunciation for a Word - `https://random-words-api.vercel.app/pronounce/`
1518

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

1821
## 🎛 Route Options
1922

2023
- Base URL: `https://random-words-api.vercel.app/word`
24+
2125
```html
22-
- /noun
23-
- /sentence
24-
- /question
25-
- /adjective
26-
- /idiom
27-
- /verb
28-
- /letter
29-
- /paragraph
30-
- /vocabulary
31-
- /1-word-quotes
32-
- /2-word-quotes
33-
- /3-word-quotes
34-
- /affirmation
26+
- /noun - /sentence - /question - /adjective - /idiom - /verb - /letter -
27+
/paragraph - /vocabulary - /1-word-quotes - /2-word-quotes - /3-word-quotes -
28+
/affirmation
3529
```
3630

3731
## 🌐 Sample API Response
@@ -47,6 +41,7 @@
4741
```
4842

4943
## 🌐 Sample Dutch Response
44+
5045
```json
5146
[
5247
{
@@ -57,12 +52,27 @@
5752
]
5853
```
5954

60-
## 🌐 Sample Pronunciation Response
55+
## 🌐 Sample Pronunciation Request
6156

6257
```json
63-
"helo worlt"
58+
POST http://localhost:3000/pronounce
59+
Content-Type: application/json
60+
61+
{
62+
"word": "This is amazing"
63+
}
6464
```
6565

66+
## 📣 Sample Pronunciation Response
67+
68+
```json
69+
{
70+
"pronunciation": "this is amasink"
71+
}
72+
```
73+
74+
- Check [api.rest](/api.rest) file for more details
75+
6676
## 💡 Learn New word
6777

6878
- [Join Telegram Channel](https://t.me/learnwordoftheday)
@@ -88,42 +98,44 @@ yarn start
8898

8999
OneClick Deploy on Heroku
90100

91-
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/mcnaveen/Random-Words-API)
101+
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/mcnaveen/Random-Words-API)
92102

93103
## 🔀 Deploying to Vercel
94104

95105
OneClick Deploy on Vercel
96106

97-
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https%3A%2F%2Fgithub.com%2Fmcnaveen%2FRandom-Words-API.git)
107+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https%3A%2F%2Fgithub.com%2Fmcnaveen%2FRandom-Words-API.git)
98108

99109
OR
100110

101111
```html
102-
- Fork the Repo
103-
- Login to Vercel (https://vercel.com/dashboard)
104-
- Click Import Project
105-
- Give Forked Repo URL
106-
- Go Live
112+
- Fork the Repo - Login to Vercel (https://vercel.com/dashboard) - Click Import
113+
Project - Give Forked Repo URL - Go Live
107114
```
108115

109116
## ✍️ Commit
110117

111118
- Stage all changes
119+
112120
```
113121
git add .
114-
```
122+
```
123+
115124
- Commit the changes
125+
116126
```
117127
yarn commit
118128
```
129+
119130
- Push the changes to GitHub
131+
120132
```
121133
git push <TO YOUR FORK>
122134
```
123135

124136
## ⚛ CREDITS
125137

126-
- Thanks to [Santhosh Veer](https://github.com/mskian) for Progressive Web APP - [Source Code](https://github.com/mskian/vue-random-words)
138+
- Thanks to [Santhosh Veer](https://github.com/mskian) for Progressive Web APP - [Source Code](https://github.com/mskian/vue-random-words)
127139

128140
## ☑ LICENSE
129141

@@ -156,4 +168,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
156168

157169
<!-- ALL-CONTRIBUTORS-LIST:END -->
158170

159-
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
171+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

api.rest

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## POST
2+
POST http://localhost:3000/pronounce
3+
Content-Type: application/json
4+
5+
{
6+
"word": "This is amazing"
7+
}

routes/pronounce.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@ import express from "express";
22
import { pronounce } from "node-pronounce";
33
const router = express.Router();
44

5-
router.get("/:word", function (req, res) {
5+
router.use(express.json());
6+
7+
router.post("/", function (req, res) {
68
res.header("Access-Control-Allow-Origin", "*");
7-
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
9+
res.header(
10+
"Access-Control-Allow-Headers",
11+
"Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With"
12+
);
813
res.header("Access-Control-Allow-Methods", "GET");
914
res.header("X-Frame-Options", "DENY");
1015
res.header("X-XSS-Protection", "1; mode=block");
1116
res.header("X-Content-Type-Options", "nosniff");
1217
res.header("Strict-Transport-Security", "max-age=63072000");
1318
res.setHeader("Content-Type", "application/json");
1419

15-
var userWord = encodeURIComponent(req.params.word) || "Automation";
16-
let wordData = pronounce(userWord);
17-
var pronouncedWord = decodeURIComponent(wordData);
18-
res.status(200).json(pronouncedWord);
19-
20+
var userWord = encodeURIComponent(req.body.word);
21+
let wordData = pronounce(userWord);
22+
const pronouncedWord = decodeURIComponent(wordData);
23+
res.status(200).json({ word: pronouncedWord });
2024
});
2125

22-
export default router;
26+
export default router;

0 commit comments

Comments
 (0)