My First REST API » The Users Feature

My First REST API » The Users Feature

The Users Feature

Now it's time to build a dynamic feature that is capable of storing some state, and offer an API to read it and modify it.

We are going to build a CRUD API over a list of users. To keep things simple, we will keep the state in the process' memory (in the simple form of an Array) and each user will be a simple string.

The goal is to study how we can leverage in ForrestJS' context API to handle a dynamic memory allocation that is safely scoped to the running App.

Create a context for the Users Feature

The first goal to achive is to allocate a piece of memory where to store our list of users. Here are some requirements for this goal:

  • this memory should be scoped to the App's and not leaking to the rest of the NodeJS process
  • this memory should be accessible to the REST handlers that we will implement later on
  • we should use reliable API, and do not play with weird Javascript black magic tricks

With that in mind, let's create the feature's manifest and register the first action in /users/index.js:

module.exports = {
  name: 'usersFeature',
  target: '$INIT_FEATURES',
  handler: ({ setContext }) => {
    setContext('users.list', ['Luke Skywalker', 'Ian Solo']);

The $INIT_FEATURES extension is part of a ForrestJS App lifecycle.
It allows to inject custom logic during the booting sequence of our App.

In our specific case, we use it to inizialize the Users Database with some dummy data from Star Wars.

The setContext() utility facilitates the definition of an App scoped piece of memory. Later on, we will be able to read from this memory using the getContext() function.

List Users Route

Now that we have the "database" in place, it is time to create a route handler that is able to expose this data to the public.

Let's create ./routes/list-users.js:

module.exports = (request, reply) => {
  const users = request.getContext('users.list');

As we saw in the Home Page's tutorial it is possible to define a Fastify's route handler as a standalone Javascript module. And ForrestJS decorates Fastify's request object with its own API, making it easy to deal with the rest of the application.

The logic here is trivial:

  1. get the users list from the App's context
  2. send it out as JSON (which is the default behavior in Fastify)

The last bit is to integrate this route handler with Fastify Service in the Feature's manifest:

const listUsersRoute = require('./routes/list-users');

// Export the Feature's Manifest: = 'usersFeature';
exports.register = () => [
  // (or register to other Actions)

  // Add routes definitions into the Fastify App:
    target: '$FASTIFY_ROUTE',
    // This could be one single object, or an array of routes:
    handler: {
      method: 'GET',
      url: '/users',
      handler: listUsersRoute,

Please note that here we are using a different Fastify hook in this example: $FASTFY_ROUTE.
This hooks let us setup a complete route definition as in fastify.route(), which is actually called under the hood.

💻 Live on CodeSandbox:

results matching ""

    No results matching ""