-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (66 loc) · 1.82 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const express = require("express");
const Joi = require("joi");
const morgan = require("morgan");
const cors = require("cors");
const CS2CS2 = require("./CS2CSWrapper");
const app = express();
// Enable CORS
app.use(cors());
// Enable JSON parsing for req.body
app.use(express.json());
// Enable logging for production
app.use(
morgan("combined", {
skip: function (req, res) {
if (req.url === "/") {
return true;
}
return false;
},
})
);
// Create a route for GET / that returns a message "Hello World"
app.get("/", (req, res) => {
res.send("Hello World");
});
app.post("/cs2cs", async (req, res) => {
try {
// Joi validation for req.body
const schema = Joi.object({
fromCrs: Joi.string().required(),
toCrs: Joi.string().required(),
precision: Joi.number().optional(),
coordinates: Joi.array()
.items(
Joi.object({
x: Joi.number().required(),
y: Joi.number().required(),
z: Joi.number().optional(),
})
)
.required(),
});
const { error, value } = schema.validate(req.body);
if (error) {
res.status(400).json({ error: true, message: error.message });
return;
}
// Get the cs2cswrapper
const cs2cs = new CS2CS2(value.fromCrs, value.toCrs, value.precision);
// Transform the coordinates
const transformedCoordinates = await Promise.all(
value.coordinates.map((coordinate) =>
cs2cs.transformPoint(coordinate.x, coordinate.y, coordinate.z)
)
);
res.status(200).json({
count: transformedCoordinates.length,
coordinates: transformedCoordinates,
});
} catch (error) {
res.status(500).json({ error: true, message: error.message });
}
});
app.listen(8081, () => {
console.log("Listening on port 8081");
});