Static Repository Example
FaasPlus allows you to easily serve static data and apply filters or transformations as needed. This approach is especially useful when you need to expose a simple dataset through a service. Let’s look at how to create a static repository of cities organized by country.
const handleRequest = async () => {
const cities = {
"United States": [
"New York City",
"Los Angeles",
"Chicago",
"Houston",
"Miami"
],
"United Kingdom": [
"London",
"Manchester",
"Birmingham",
"Glasgow",
"Edinburgh"
],
"France": [
"Paris",
"Lyon",
"Marseille",
"Toulouse",
"Nice"
],
"Germany": [
"Berlin",
"Munich",
"Hamburg",
"Frankfurt",
"Cologne"
],
"Japan": [
"Tokyo",
"Osaka",
"Kyoto",
"Yokohama",
"Fukuoka"
]
};
return cities;
};
{
"France": [
"Paris",
"Lyon",
"Marseille",
"Toulouse",
"Nice"
],
"Germany": [
"Berlin",
"Munich",
"Hamburg",
"Frankfurt",
"Cologne"
],
"Japan": [
"Tokyo",
"Osaka",
"Kyoto",
"Yokohama",
"Fukuoka"
],
"United Kingdom": [
"London",
"Manchester",
"Birmingham",
"Glasgow",
"Edinburgh"
],
"United States": [
"New York City",
"Los Angeles",
"Chicago",
"Houston",
"Miami"
]
}
In this example, the service returns a static set of cities organized by country. You can use this service to retrieve and display city information without a dynamic data source.
To extend this service, let’s add filtering based on user input, such as returning cities only from a specific country.
Filtering Example
Here, we’ll modify the service to return cities only for a specified country, using a filter parameter.
const handleRequest = async (req) => {
const cities = {
"United States": [
"New York City",
"Los Angeles",
"Chicago",
"Houston",
"Miami"
],
"United Kingdom": [
"London",
"Manchester",
"Birmingham",
"Glasgow",
"Edinburgh"
],
"France": [
"Paris",
"Lyon",
"Marseille",
"Toulouse",
"Nice"
],
"Germany": [
"Berlin",
"Munich",
"Hamburg",
"Frankfurt",
"Cologne"
],
"Japan": [
"Tokyo",
"Osaka",
"Kyoto",
"Yokohama",
"Fukuoka"
]
};
const filteredCities=cities[req.params.country];
return filteredCities;
};
[
"Paris",
"Lyon",
"Marseille",
"Toulouse",
"Nice"
]
With this extension, the service now accepts a country
parameter to filter and return cities from that
country. You can further build on this example to add advanced filtering or transformation logic. Note that the
req
object is essential for retrieving parameters for filtering, as it provides the necessary
information based on the request method.