-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
45 lines (40 loc) · 1.53 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
// Importing Libraries
const math = require('mathjs');
const express = require('express');
// Setting Up Express App and Ports
const app = express();
const port = 3000;
// Tells Express to use the /static folder for static files
app.use(express.static('static'));
// Setting HTTP Code and Magnitude Formula as Constants
const bad = 400;
const magform = math.parse('(x-0)^2 + (y-0)^2');
// Radians to Degrees Function
const rd = (n) => {
let num = math.unit(n, "radians");
num = num.to("degrees");
return num.toJSON().value;
}
// Sends the User the HTML page when they connect to the root
app.get('/', async (req, res) => {
res.sendFile("static/index.html", { root: __dirname });
});
// Magnitude and Direction Calculator
app.get('/magdir', async (req, res) => {
//Checks if the sender sent both x and y parameters, and returns a 400 status code if not
if (!req.query.x || !req.query.y) {
res.sendStatus(bad);
} else {
// setting x and y to variables
let x = math.abs(req.query.x);
let y = math.abs(req.query.y);
// Finding the magnitude of the vector, without square roots
let mag = magform.evaluate({x:x, y:y});
// Inverse tangent of the triangle and converts the radian result to degrees
let dir = math.atan(y/x);
dir = rd(dir);
// Sends back an object with the magnitude and direction
res.json({mag:mag, dir:dir});
}
});
app.listen(port, () => console.log(`listening on http://localhost:${port}`));