Controllerclass, or any existing derivation of the
Controllerclass. Here we have defined a controller that will act as the corresponding controller from the message router from our previous examples.
Actionclass. The name of the method represents the name of the action. This is the name that the router binds with when defining its reactions for a given path.
messagecontroller. Let's complete this example by implementing the
messagecontroller for creating the message. This action is responsible for handling
POST /messagesrequests as defined in the message router.
Messagewhich is a Wrapper Facade for each message we create. We then add a
messagesproperty to our controller. This will be used to store the messages we create. If you remember the discussion about object-like properties in the object model, then you will remember that we cannot initialize an array property when we define it. Instead, we must initialize the property in the
init()method. In this case, we initialize the
messagesproperty to an empty array.
create()action to create the message, which is located in
req.body. We are expecting the data for the message to be under the
messageenvelope. To create the message, we first compute the id of the next message using
_nextId. We then create a data object, and use this data object to create a
Messageobject. Last, we push the message object unto the collection of messages, and return a response to the client.
schemaproperty. Now that we have enabled request input validation, the
execute(req, res)method will only be called if validation succeeds. This means there is no need to add validation logic to the
GET /messages/:messageId). This path for this route had a router parameter named
messageId. The parameter in the path is available on the
req.paramsobject. To illustrate how we can use this parameter in our action, below is the implementation of the
getOne()action on the
getOne()method above, the action for this method defines a schema to validate the expected parameter. The action then uses the
messageIdparameter to search for the message that has an id that matches. If the message is found, the message is returned as the response. Otherwise, the action returns a 404 response.
messagecontroller with no action in the router, it will use the
Actionclass. Because the return value is a class and not an object, we can port an action classes to different controller actions. We can also extend an action class to create a more domain-specific action.
onUploadComplete(req, res)method, which is notified when the upload is complete.