A REST API used to parse, simplify, and evaluate mathematical expressions.

/5-equation-solver directory.npm i to install the necessary dependencies..env.sample to .env and set PORT to the port you’d like the server to listen for requests on. If no PORT is provided, port 8080 will be used.npm start to start listening for requests on http://localhost:<PORT>.The API has 3 endpoints:
Each endpoint represents a step in the expression evaluation process, so each endpoint will build on the previous.
I.e.,
parsewill return the parsed expression,simplifywill return the parsed expression and the simplified expression, andevaluatewill return the parsed expression, simplified expression, and the result.
In general, all requests should be GET requests, and request URLs should be built like so:
http://localhost:<PORT>/<ENDPOINT>?expression=<EXPRESSION>&<VAR1>=<VAL1>&<VAR2>=<VAL2>&...
where
<PORT> is the port the server is listening on (8080 by default)<ENDPOINT> is one of three endpoints: parse, simplify, or evaluate<EXPRESSION> is a URI-encoded mathematical expression
<VAR>=<VAL> are variable definitions, or key value pairs representing the scope of the expression
evaluate endpointFor example, say we want to evaluate the expression x*y where x=2 and y=3. We would send a GET request to:
http://localhost:8080/evaluate?expression=x*y&x=2*y=3
or, using LaTeX:
http://localhost:8080/evaluate?expression=x%20%5Ctimes%20y&x=2&y=3
/parseReturns the parsed expression as string or tex.
http://localhost:8080/parse?expression=x*x
fetch('http://localhost:8080/parse?expression=x*x')
.then(res => res.json())
.then(data => {
console.log(data);
});
All responses are JSON objects with the following data:
{
"valid": true,
"error": "",
"input": {
"expression": "x*x",
"scope": {}
},
"parsed": {
"string": "x * x",
"tex": " x\\cdot x"
}
}
"Request Error: No expression provided"
expression was provided in the query"Parsing Error: Invalid expression"
/simplifyReturns the parsed expression as string or tex and the simplified expression as string or tex.
http://localhost:8080/simplify?expression=x*x
fetch('http://localhost:8080/simplify?expression=x*x')
.then(res => res.json())
.then(data => {
console.log(data);
});
All responses are JSON objects with the following data:
{
"valid": true,
"error": "",
"input": {
"expression": "x*x",
"scope": {}
},
"parsed": {
"string": "x * x",
"tex": " x\\cdot x"
},
"simplified": {
"string": "x ^ 2",
"tex": "{ x}^{2}"
}
}
"Request Error: No expression provided"
expression was provided in the query"Parsing Error: Invalid expression"
/evaluateReturns the parsed expression as string or tex, the simplified expression as string or tex, and the result given the scope provided.
http://localhost:8080/evaluate?expression=x*x&x=2
fetch('http://localhost:8080/evaluate?expression=x*x&x=2')
.then(res => res.json())
.then(data => {
console.log(data);
});
All responses are JSON objects with the following data:
{
"valid": true,
"error": "",
"input": {
"expression": "x*x",
"scope": {
"x": 2
}
},
"parsed": {
"string": "x * x",
"tex": " x\\cdot x"
},
"simplified": {
"string": "x ^ 2",
"tex": "{ x}^{2}"
},
"result": 4
}
"Request Error: No expression provided"
expression was provided in the query"Parsing Error: Invalid expression"
"Evaluation Error: " + error.message
| Attribute | Type | Endpoint | Description |
|---|---|---|---|
res.valid |
boolean |
parse | simplify | evaluate |
Indicates whether or not the expression provided is valid. |
res.error |
string |
parse | simplify | evaluate |
Indicates whether or not an error occurred and, if so, why. If no error occurred, this will be an empty string |
res.input |
object | undefined |
parse | simplify | evaluate |
Indicates the expression and scope values that were provided. |
res.input.expression |
string |
parse | simplify | evaluate |
The expression that was provided |
res.input.scope |
object |
parse | simplify | evaluate |
The scope (variable definitions) that were provided. Each key value pair corresponds to a variable defined in the scope. |
res.parsed |
object | undefined |
parse | simplify | evaluate |
The parsed expression. If a parsing error occurred, res.parsed will be undefined. |
res.parsed.string |
string |
parse | simplify | evaluate |
The string representation of the parsed expression |
res.parsed.tex |
string |
parse | simplify | evaluate |
The LaTeX representation of the parsed expression |
res.simplified |
object | undefined |
simplify | evaluate |
The simplified expression. If a parsing error occurred, res.simplified will be undefined. |
res.simplified.string |
string |
simplify | evaluate |
The string representation of the simplified expression |
res.simplified.tex |
string |
simplify | evaluate |
The LaTeX representation of the simplified expression |
res.result |
number | string | undefined |
evaluate |
The result of the evaluation. For regular values, this will be of type number. But for irregular values (i.e. complex numbers), the result will be converted to string. If an evaluation error occurred, res.result will be undefined. |