-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (58 loc) · 1.7 KB
/
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
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
const express = require("express");
const path = require("path");
const axios = require("axios");
const cors = require("cors");
const { parse } = require("./src/utils");
const Word = require("./src/models/word.model");
require("./src/db");
const app = express();
//Getting variable for the environment.
const PORT = process.env.PORT;
const APP_ID = process.env.APP_ID;
const APP_KEY = process.env.APP_KEY;
const staticRoot = path.join(__dirname, "client", "build");
//applying middlewares
app.use(cors());
app.use(express.json());
app.use(express.static(staticRoot));
app
.route("/api/words")
.get(async (req, res, next) => {
try {
//Fetching all words from the database.
const words = await Word.find().sort("-createdAt");
return res.json(words);
} catch (err) {
res.status(500).json({ error: err.message });
}
})
.post(async (req, res, next) => {
try {
const { word } = req.body;
//Fetching the new word from the Oxford API.
const data = (
await axios.get(
`https://od-api.oxforddictionaries.com/api/v2/entries/en-gb/${word}`,
{
headers: {
app_id: APP_ID,
app_key: APP_KEY,
},
}
)
).data;
//Creating a new word in mongoDB.
const createdWord = await Word.create({
word: data.id,
//parsing the incoming data to store relevant information.
results: parse(data),
});
return res.json(createdWord);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get("/*", (req, res) => {
res.redirect("/index.html");
});
app.listen(PORT, () => console.log(`Server Up and Running on ${PORT}`));