Creating a basic RESTful web service
As a first step, Listing 14.9 creates a web service that implements some of the combinations of URL and HTTP methods described in Table 14.4.
Listing 14.9: Creating a basic web service in the index.ts file in the src/server/api folder
import { Express } from "express";
import repository from "../data";
export const createApi = (app: Express) => {
app.get("/api/results", async (req, resp) => {
if (req.query.name) {
const data = await repository.getResultsByName(
req.query.name.toString(), 10);
if (data.length > 0) {
resp.json(data);
} else {
resp.writeHead(404);
}
} else {
resp.json(await repository.getAllResults(10));
}
resp.end();
});
}
The listing shows how easy it is to create an API for clients by repurposing the parts of the application...