This microservice is a little express server that takes an HTTP POST request.
Node.js and a dream
-
To begin, clone the repository into a new folder.
-
Run
npm install
inside that folder.
To change the port the server runs on, edit the corresponding line in the config.json
file.
To start the server, run npm start
.
By default, all requests are to be made to http:/127.0.0.1:3000/
as HTTP POSTs.
Send your data in the request body and include application/json
as the Content-Type
header.
Request bodies are expected to follow the following format:
{
tile_costs: [[Number, Number]], // Initial, Monthly
tile_layout: [[Number, ...]], // Tile ID
selection_topleft: [Number, Number] || undefined, // x, y; Optional
selection_bottomright: [Number, Number] || undefined, // x, y; Optional
}
If the server understood your request and found no validation errors, an HTTP response will be returned:
{
price_initial: Number,
price_monthly: Number,
tiles: [
{
quantity: Number,
initial: Number,
monthly: Number
},
],
}
The server will repeat json contained in requests and responses to stdout.
If there is invalid syntax in a request, the server will send a message to stderr with details as to any attempted wrongdoing and send back an HTTP 400
error in response.
Sample request body:
{
tile_costs: [
[25, 10],
[10, 0],
[5, 5],
],
tile_layout: [
[0, 1, 0],
[0, 0, 0],
[2, 2, 2],
],
selection_topleft: [0, 0],
selection_bottomright: [1, 1],
}
Sample response:
{
price_initial: 85,
price_monthly: 30,
tiles: [
{ quantity: 3, initial: 75, monthly: 30 },
{ quantity: 1, initial: 10, monthly: 0 },
],
}
Example programmatic request / response code:
fetch("http://127.0.0.1:3000/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(/* Your Request Here... */),
})
.then((res) => res.json())
.then((data) => {
// Handle data...
})
.catch((err) => {
console.log(`Fetch Error: ${err}`);
});