This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
88 lines (66 loc) · 1.87 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
const express = require('express')
const path = require('path')
const fs = require('fs')
const request = require('request')
const osmtogeojson = require('osmtogeojson')
const port = process.env.PORT || 8080;
const app = express();
/**
* Useful function to create folder if nonexistent
* @param {} filePath
*/
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
app.use(express.static(__dirname));
app.use(express.static(__dirname + '/dist'));
/**
* Give the all the nuclear central in geojson
*/
app.get('/nuclearcentral', (req, res) => {
let filepath = __dirname + '/data/nuclearcentralmap.geojson';
if (fs.existsSync(filepath)) {
fs.readFile(filepath, (err, data) => {
if (err)
res.send(err)
res.send(JSON.parse(data))
})
} else {
let query = '[out:json];(way["generator:source"="nuclear"];);out;>;out skel qt;';
request.post('https://overpass-api.de/api/interpreter', { body: query }, (err, response, body) => {
const geojson = osmtogeojson(JSON.parse(body), {
flatProperties: true
});
ensureDirectoryExistence(filepath)
fs.writeFile(filepath, JSON.stringify(geojson), (err) => {
if (err)
res.send(err)
res.send(geojson)
})
})
}
});
/**
* Give all the nuclear accidents in geojson
*/
app.get('/nuclearaccidents', (req, res) => {
let filepath = __dirname + '/data/DataNuclearAccidents.geojson';
if (fs.existsSync(filepath)) {
fs.readFile(filepath, (err, data) => {
if (err)
res.send(err)
res.send(JSON.parse(data))
})
} else {
res.send(JSON.parse("{}"))
}
});
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'index.html'));
});
app.listen(port);