Optimized and added logging
This commit is contained in:
44
index.js
44
index.js
@@ -1,7 +1,8 @@
|
|||||||
const fs = require('fs')
|
const { createReadStream, readFileSync } = require('fs')
|
||||||
const path = require('path')
|
const { join } = require('path')
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const cors = require('cors')
|
const cors = require('cors')
|
||||||
|
const morgan = require('morgan')
|
||||||
const json = require('big-json')
|
const json = require('big-json')
|
||||||
|
|
||||||
const GeoJsonGeometriesLookup = require("geojson-geometries-lookup")
|
const GeoJsonGeometriesLookup = require("geojson-geometries-lookup")
|
||||||
@@ -9,13 +10,14 @@ const lookupTable = require('./rep-lookup')
|
|||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
const PORT = 3000
|
const port = 3000
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Add modules to express app
|
||||||
app.use(cors())
|
app.use(cors())
|
||||||
|
app.use(morgan('common'))
|
||||||
|
|
||||||
app.use(express.static(path.join(__dirname, 'public')))
|
// Static files
|
||||||
|
app.use(express.static(join(__dirname, 'public')))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MAIN ROUTE
|
* MAIN ROUTE
|
||||||
@@ -26,13 +28,11 @@ app.use(express.static(path.join(__dirname, 'public')))
|
|||||||
app.use('/rep', async (req, res) => {
|
app.use('/rep', async (req, res) => {
|
||||||
let { lat, lon } = req.query
|
let { lat, lon } = req.query
|
||||||
|
|
||||||
const missingRequiredQuery = !lat || !lon
|
|
||||||
|
|
||||||
// If the query doesn't contain the required params...
|
// If the query doesn't contain the required params...
|
||||||
if (missingRequiredQuery) {
|
if (!lat || !lon) {
|
||||||
res.json({
|
res.status(401).json({
|
||||||
"message": "The request is missing geolocation data. You must use 'lat' and 'lon' in your query."
|
"message": "The request is missing geolocation data. You must use 'lat' and 'lon' in your query."
|
||||||
}).status(401).end();
|
});
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,13 +43,13 @@ app.use('/rep', async (req, res) => {
|
|||||||
const closestCity = await (await fetch(
|
const closestCity = await (await fetch(
|
||||||
`https://geo.api.gouv.fr/communes?lat=${lat}&lon=${lon}`
|
`https://geo.api.gouv.fr/communes?lat=${lat}&lon=${lon}`
|
||||||
)).json();
|
)).json();
|
||||||
let codeDepartement = String(closestCity[0]?.codeDepartement)
|
let codeDepartement = String(closestCity[0].codeDepartement)
|
||||||
|
|
||||||
// If somehow, the gov api can't find the nearest city...
|
// If somehow, the gov api can't find the nearest city...
|
||||||
if (!codeDepartement || codeDepartement === "") {
|
if (!codeDepartement || codeDepartement === "") {
|
||||||
res.json({
|
res.status(400).json({
|
||||||
"message": "The geolocation wasn't able to pinpoint the region the coords were in."
|
"message": "The geolocation wasn't able to pinpoint the region the coords were in."
|
||||||
}).status(400).end();
|
});
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,8 +57,8 @@ app.use('/rep', async (req, res) => {
|
|||||||
codeDepartement = codeDepartement.padStart(3, "0");
|
codeDepartement = codeDepartement.padStart(3, "0");
|
||||||
|
|
||||||
// Gets the associated features with a read and parse stream
|
// Gets the associated features with a read and parse stream
|
||||||
const pathToFeatures = path.join(__dirname, `public/cirs/${codeDepartement}.json`)
|
const pathToFeatures = join(__dirname, `public/cirs/${codeDepartement}.json`)
|
||||||
const readStream = fs.createReadStream(pathToFeatures)
|
const readStream = createReadStream(pathToFeatures)
|
||||||
const parseStream = json.createParseStream()
|
const parseStream = json.createParseStream()
|
||||||
|
|
||||||
parseStream.on('data', (pojo) => {
|
parseStream.on('data', (pojo) => {
|
||||||
@@ -75,25 +75,25 @@ app.use('/rep', async (req, res) => {
|
|||||||
|
|
||||||
// If no code is found, send a 404
|
// If no code is found, send a 404
|
||||||
if (!code) {
|
if (!code) {
|
||||||
res.json({
|
res.status(404).json({
|
||||||
"message": "The requested data couldn't be found. Maybe it was moved or the data isn't generated properly."
|
"message": "The requested data couldn't be found. Maybe it was moved or the data isn't generated properly."
|
||||||
}).status(404).end();
|
});
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a code is found, send the associated file containing the representant data
|
// If a code is found, send the associated file containing the representant data
|
||||||
const repFile = fs.readFileSync(path.join(__dirname, `public/reps/${lookupTable[code]}.json`), 'utf-8')
|
const repFile = readFileSync(join(__dirname, `public/reps/${lookupTable[code]}.json`), 'utf-8')
|
||||||
res.end(repFile)
|
res.end(repFile)
|
||||||
})
|
})
|
||||||
readStream.pipe(parseStream);
|
readStream.pipe(parseStream);
|
||||||
})
|
})
|
||||||
|
|
||||||
app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));
|
app.listen(port, () => console.log(`Server listening on port: ${port}`));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CUT DOWN CIRCO JSON
|
* CUT DOWN CIRCO JSON
|
||||||
*/
|
*/
|
||||||
// const f = fs.readFileSync(path.join(__dirname, 'public/circonscriptions-legislatives.json'), 'utf-8')
|
// const f = fs.readFileSync(join(__dirname, 'public/circonscriptions-legislatives.json'), 'utf-8')
|
||||||
// const data = JSON.parse(f)
|
// const data = JSON.parse(f)
|
||||||
|
|
||||||
// const features = Object.values(data.features)
|
// const features = Object.values(data.features)
|
||||||
@@ -106,7 +106,7 @@ app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));
|
|||||||
// "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
|
// "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
|
||||||
// "features": value
|
// "features": value
|
||||||
// }
|
// }
|
||||||
// fs.writeFileSync(path.join(__dirname, `public/cirs/${key}.json`), JSON.stringify(output))
|
// fs.writeFileSync(join(__dirname, `public/cirs/${key}.json`), JSON.stringify(output))
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// function groupBy(list, keyGetter) {
|
// function groupBy(list, keyGetter) {
|
||||||
|
|||||||
53
package-lock.json
generated
53
package-lock.json
generated
@@ -9,7 +9,8 @@
|
|||||||
"big-json": "^3.2.0",
|
"big-json": "^3.2.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"geojson-geometries-lookup": "^0.5.0"
|
"geojson-geometries-lookup": "^0.5.0",
|
||||||
|
"morgan": "^1.10.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@turf/bbox": {
|
"node_modules/@turf/bbox": {
|
||||||
@@ -118,6 +119,22 @@
|
|||||||
"node": ">=0.8"
|
"node": ">=0.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/basic-auth": {
|
||||||
|
"version": "2.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
|
||||||
|
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
|
||||||
|
"dependencies": {
|
||||||
|
"safe-buffer": "5.1.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/basic-auth/node_modules/safe-buffer": {
|
||||||
|
"version": "5.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
||||||
|
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
||||||
|
},
|
||||||
"node_modules/big-json": {
|
"node_modules/big-json": {
|
||||||
"version": "3.2.0",
|
"version": "3.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/big-json/-/big-json-3.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/big-json/-/big-json-3.2.0.tgz",
|
||||||
@@ -554,6 +571,32 @@
|
|||||||
"node": ">= 0.6"
|
"node": ">= 0.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/morgan": {
|
||||||
|
"version": "1.10.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz",
|
||||||
|
"integrity": "sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==",
|
||||||
|
"dependencies": {
|
||||||
|
"basic-auth": "~2.0.1",
|
||||||
|
"debug": "2.6.9",
|
||||||
|
"depd": "~2.0.0",
|
||||||
|
"on-finished": "~2.3.0",
|
||||||
|
"on-headers": "~1.0.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/morgan/node_modules/on-finished": {
|
||||||
|
"version": "2.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
|
||||||
|
"integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==",
|
||||||
|
"dependencies": {
|
||||||
|
"ee-first": "1.1.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/ms": {
|
"node_modules/ms": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||||
@@ -594,6 +637,14 @@
|
|||||||
"node": ">= 0.8"
|
"node": ">= 0.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/on-headers": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/once": {
|
"node_modules/once": {
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
|
||||||
|
|||||||
@@ -6,7 +6,8 @@
|
|||||||
"big-json": "^3.2.0",
|
"big-json": "^3.2.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"geojson-geometries-lookup": "^0.5.0"
|
"geojson-geometries-lookup": "^0.5.0",
|
||||||
|
"morgan": "^1.10.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node index.js"
|
"start": "node index.js"
|
||||||
|
|||||||
Reference in New Issue
Block a user