-
Notifications
You must be signed in to change notification settings - Fork 911
Expand file tree
/
Copy pathcontroller.js
More file actions
38 lines (32 loc) · 1.12 KB
/
controller.js
File metadata and controls
38 lines (32 loc) · 1.12 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
const axios = require('axios')
const nameData = require('./store')
// Function to find list of the countries
exports.getCountries = async (req,res)=> {
try {
let result = await axios.get('https://restcountries.com/v3.1/all')
let finalResult = result.data.map((val) => {
return {Countryname: val.name.common,currencies:val.currencies}
})
console.log(finalResult)
res.status(200).send({status:"ok", data: finalResult})
} catch (error) {
console.log(error)
}
}
// Function to find the names of specific patter
exports.searchName = async (req,res) => {
try {
let nameRegex = /^[A-Za-z]+$/;
let input = req.query.input;
if(!input.match(nameRegex)){
res.status(400).send({error:"please give only albhabet in request query"})
}
let result = nameData.nameData.filter(val => {
return val.toLowerCase().startsWith(input.toLowerCase())
})
console.log("result", result)
res.status(200).send({status:"ok", data: result})
} catch (error) {
console.log('error', error)
}
}