Today we are taking our webserver/api to the next level. We are including support to query our data object to only pull back the record we are after.
//Install express and use in app
var express = require("express");
var app = express();
//Seed Data as json
const data = require("./data.json");
//GET request for data.json from main page
app.get("/:id", function (req, res) {
res.send(data[req.params.id - 1]);
console.log(`[${new Date().toISOString()}] new request:`);
console.log("Request Method is: " + req.method);
console.log("Request Url is: " + req.url);
});
//Send main page by any means
app.all("/", function (req, res) {
res.send("hello!");
console.log(`[${new Date().toISOString()}] new request:`);
console.log("Request Method is: " + req.method);
console.log("Request Url is: " + req.url);
});
//Go Live with Server
app.listen("1234", function () {
console.log("running: http://localhost:1234/");
});
And there we have it! we can now mage an HTTP GET request to
//GET: http://localhost:1234/2
//output:
{
"id":2,
"firstName":"Jane",
"lastName":"Doe",
"age":40,
"eyeColor":"green"
}
So the original post here was going to tie in earlier array methods to achieve the same thing but it didn’t work! So I had to go back to the drawing board.
Anyhow, after a little investigation, we can indeed use array.Find()!
The issue was type!!! I think this is the first genuine use case for typescript I’ve come across. So I should look into it in the future.
New and improved GET endpoint:
//GET request for data.json from main page
app.get("/:id", function (req, res) {
res.send(data.find(({ id }) => id == req.params.id));
console.log(`[${new Date().toISOString()}] new request:`);
console.log("Request Method is: " + req.method);
console.log("Request Url is: " + req.url);
});
The same output applies, but this time you can change the array index and the result will be as we actually want it!
//GET: http://localhost:1234/2
//output:
{
"id":2,
"firstName":"Jane",
"lastName":"Doe",
"age":40,
"eyeColor":"green"
}