-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (49 loc) · 1.47 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
const express = require('express');
const app = express();
const cors = require('cors');
const dataService = require('./database-service');
const PORT = 3000;
app.use(cors());
// GET All states
app.get('/state', (req, res) => {
dataService.getStates()
.then((results) => {
res.status(200).json(results);
}, () => {
res.status(404).json({error: "Something went wrong."});
});
});
// GET 1 State by Name
app.get('/state/:stateName', (req, res) => {
const stateName = req.params.stateName;
dataService.getStateByName(stateName)
.then((results) => {
res.status(200).json(results);
}, () => {
res.status(404).json({error: "Something went wrong."});
});
});
// GET all counties for 1 state
app.get('/state/:stateName/county', (req, res) => {
const stateName = req.params.stateName;
dataService.getCounties(stateName)
.then((results) => {
res.status(200).json(results);
}, () => {
res.status(404).json({error: "Something went wrong."});
});
});
// GET 1 county by stateName
app.get('/state/:stateName/county/:countyName', (req, res) => {
const stateName = req.params.stateName;
const countyName = req.params.countyName;
dataService.getCounty(stateName, countyName)
.then((results) => {
res.status(200).json(results);
}, () => {
res.status(404).json({error: "Something went wrong."});
});
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});