-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
38 lines (30 loc) · 952 Bytes
/
start.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
import express from 'express';
import fs from 'fs';
import cors from 'cors';
import { parse } from 'csv-parse/sync';
// start
const app = express();
// routes
app.get('/', (req, res) =>
res.json({
status: 'ok',
version: process.env.npm_package_version,
readme: 'https://github.com/rubenvar/codigos-postales',
})
);
app.get('/api/cp/:cp', cors(), async (req, res) => {
const { cp } = req.params;
const data = fs.readFileSync('./assets/codigos_postales_municipios.csv', {
encoding: 'utf8',
});
const json = parse(data, { columns: true });
const munCodeObj = json.find((obj) => obj.codigo_postal === cp);
if (!munCodeObj) return res.json({ error: `Object not found for cp ${cp}` });
return res.json(munCodeObj);
});
// port
app.set('port', process.env.PORT || 4321);
// 🚀 go
const server = app.listen(app.get('port'), () => {
console.log(`Express running → http://localhost:${server.address().port}`);
});