Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node assignment uttam #263

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Uttam_node_assignment/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)

}
}
16 changes: 16 additions & 0 deletions Uttam_node_assignment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const controller = require('./controller')


const app = express();

app.use(cors());
app.use(bodyParser.json());

app.get('/countries', controller.getCountries);
app.get('/search', controller.searchName)


app.listen(3000, () => console.log("server listening on port 3000"))
19 changes: 19 additions & 0 deletions Uttam_node_assignment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "node_assignment",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon ./index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.3.5",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.18.2",
"nodemon": "^2.0.22"
}
}
Loading