This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
168 lines (146 loc) · 4.14 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const express = require("express");
const rateLimit = require("express-rate-limit");
const morgan = require("morgan");
const cors = require("cors");
const helmet = require("helmet");
const listEndpoints = require("express-list-endpoints");
const db = require("./db");
const { Egon } = require("./db/Egon");
const regions = require("./regions.json");
const { HTTPError, handleError } = require("./error");
(async () => {
const app = express();
await db.sync();
console.log("connected to db");
app.use(express.json());
app.use(morgan("tiny"));
app.use(cors());
app.use(helmet());
app.set("trust proxy", 1);
app.use(
rateLimit({
windowMs: 10 * 60 * 1000,
max: 100,
handler: (req, res) => handleError(429, "too many requests", res),
})
);
// Regions query.
app.get("/regions", (req, res) => {
res.json(Object.keys(regions));
});
// Provinces query (by region):
app.get("/:region/provinces", (req, res) => {
try {
const provinces = regions[req.params.region].provinces;
res.json(provinces);
} catch (_) {
handleError(
404,
"could not find any provinces for the provided region",
res
);
}
});
// Cities query (by province).
app.get("/:province/cities", async (req, res) => {
try {
const cities = await Egon.findAll({
attributes: ["city"],
group: ["city"],
where: { province: req.params.province },
});
res.json(cities.map((obj) => obj.city));
} catch (error) {
handleError(
500,
"error retrieving cities for the provided province",
res
);
}
});
// Streets query (by province and city).
// Slicing is done on client (for performance reasons).
app.get("/:province/:city/streets", async (req, res) => {
try {
const { province, city } = req.params;
const streets = await Egon.findAll({
where: { province, city },
attributes: ["street"],
group: ["street"],
});
res.json(streets.map((obj) => obj.street));
} catch (error) {
handleError(500, "error retrieving streets for the provided city", res);
}
});
// Numbers query (by province, city, street)
app.get("/:province/:city/:street/numbers", async (req, res) => {
try {
const { province, city, street } = req.params;
let numbers = await Egon.findAll({
where: { province, city, street },
attributes: ["number", "egon"],
});
// Sort numbers.
numbers
.sort((a, b) => {
if (a.number === "SNC") {
return -1;
}
if (b.number === "SNC") {
return 1;
}
return (
a.number.length - b.number.length ||
a.number.localeCompare(b.number)
);
})
.sort((a, b) => {
const aNum = Number(a.number.split("/")[0]);
const bNum = Number(b.number.split("/")[0]);
return aNum - bNum;
});
res.json(numbers);
} catch (error) {
handleError(500, "error retrieving numbers for this street", res);
}
});
// Final query (egon data).
app.get("/egon", async (req, res) => {
try {
if (!req.query.id) {
throw new HTTPError(400, "you must provide the egon id");
}
const data = await Egon.findOne({
where: { egon: req.query.id },
});
if (!data) {
throw new HTTPError(404, "no data available for this egon");
}
res.json(data);
} catch (error) {
handleError(
error.statusCode || 500,
error.message || "error retrieving streets for the provided city",
res
);
}
});
app.get("/", (req, res) => {
const endpoints = listEndpoints(app).flatMap(({ path, methods }) =>
!(path === "*" || path === "/")
? {
path,
methods,
query: path === "/egon" ? ["id"] : undefined,
}
: []
);
res.json(endpoints);
});
// Catch all errors.
app.get("*", (req, res) => {
res.status(404).json({ error: "route not found" });
});
app.listen(5000, () => console.log("server started on port 5000"));
})();