-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (52 loc) · 1.28 KB
/
app.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
var countriesData = require('./countries.js');
countriesData = countriesData.data.country;
const Fuse = require("fuse.js");
const express = require("express");
const app = express();
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
app.get('/', (req, res) => {
res.redirect('https://github.com/d3vkk/transcountriesapi');
})
app.get('/name', (req, res) => {
const fuse = new Fuse(countriesData, {
keys: ["name"],
});
const searchResults = fuse.search(req.query.name);
if (searchResults != []) {
res.json({
found: true,
message: 'Country found',
country: searchResults[0].item
});
} else {
res.json({
found: false,
message: 'Country NOT found',
country: [],
});
}
})
app.get('/tel', (req, res) => {
const fuse = new Fuse(countriesData, {
keys: ["tel"],
});
const searchResults = fuse.search(req.query.tel);
if (searchResults != []) {
res.json({
found: true,
message: 'Country found',
country: searchResults[0].item,
});
} else {
res.json({
found: false,
message: 'Country NOT found',
country: [],
});
}
})
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`App running on 127.0.0.1:${port}`));