-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
83 lines (70 loc) · 3.01 KB
/
server.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require('dotenv').config()
const express = require('express');
const path = require('path');
const axios = require('axios');
const fs = require("fs");
const contents = fs.readFileSync("physicians.json");
const physiciansObj = JSON.parse(contents);
const app = express();
const apiKey = process.env.API_KEY
console.log(apiKey)
const port = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
})
app.get('/api/find', (req, res) => {
let name = req.query['name']
console.log(req.query)
name_list = name.split(" ")
let phy = {}, first_name = '', mid_name = '', last_name = ''
const len = name_list.length
if(len >= 3){
if(len === 3){
if(name_list[len-1].toUpperCase() === 'JR' || name_list[len-1].toUpperCase() === 'SR'){
first_name = name_list[0].toUpperCase()
mid_name = ''
last_name = name_list[1].toUpperCase()
phy = physiciansObj.filter(p => p.first_name.toUpperCase() === first_name && p.mid_name.toUpperCase() === mid_name && p.last_name.toUpperCase() === (last_name + ' JR' || ' SR'))
}else{
first_name = name_list[0].toUpperCase()
mid_name = name_list[1].toUpperCase()
last_name = name_list[2].toUpperCase()
phy = physiciansObj.filter(p => p.first_name.toUpperCase() === first_name && p.mid_name.toUpperCase() === mid_name && p.last_name.toUpperCase() === last_name)
}
}else if(len === 4){
first_name = name_list[0].toUpperCase()
mid_name = name_list[1].toUpperCase()
last_name = name_list[2].toUpperCase()
phy = physiciansObj.filter(p => p.first_name.toUpperCase() === first_name && p.mid_name.toUpperCase() === mid_name && p.last_name.toUpperCase() === (last_name + ' JR' || ' SR'))
}
}else if(name_list.length == 2){
first_name = name_list[0].toUpperCase()
last_name = name_list[1].toUpperCase()
phy = physiciansObj.filter(p => p.first_name.toUpperCase() === first_name && p.mid_name.toUpperCase() === mid_name && p.last_name.toUpperCase() === last_name)
}else{
res.send('')
return
}
if(phy.length === 0){
res.send('')
return
}
const location = `${phy[0].address}, ${phy[0].city}, ${phy[0].state}`
console.log(location)
const encodedAddr = encodeURIComponent(location)
const geoURL = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddr}&key=${apiKey}`
axios.get(geoURL).then((result) => {
if (result.data.status === 'OK') {
const geo_info = result.data.results[0]['geometry']['location']
res.send(geo_info)
return
}
})
})
app.get('/api/physicians', (req, res) => {
res.sendFile(path.join(__dirname, 'physicians.json'))
})
app.listen(port, () => {
console.log('server is up on ' + port);
});