app/controllers
.Controller
class, or any existing derivation of the Controller
class. Here we have defined a controller that will act as the corresponding controller from the message router from our previous examples.extend()
method to define the actions what actions the controller supports.Action
class. 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.message
controller. Let's complete this example by implementing the message
controller. message
controller for creating the message. This action is responsible for handling POST /messages
requests as defined in the message router.execute(req, res)
method, which is responsible for handling the request. The execute(req, res)
method must return null
, undefined
, or a Promise
. The req
parameter is an HTTP request object, and the res
parameter is a HTTP response object.Message
which is a Wrapper Facade for each message we create. We then add a messages
property 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 messages
property 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 message
envelope. 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 Message
object. Last, we push the message object unto the collection of messages, and return a response to the client.validate(req)
method.schema
property. 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 execute(req, res)
method.GET /messages/:messageId
). This path for this route had a router parameter named messageId
. The parameter in the path is available on the req.params
object. To illustrate how we can use this parameter in our action, below is the implementation of the getOne()
action on the message
controller.getOne()
method above, the action for this method defines a schema to validate the expected parameter. The action then uses the messageId
parameter 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.message
controller with no action in the router, it will use the __invoke()
definition.Action
class. 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.SingleViewAction
Specialization of the ViewAction class that only supports a single view. Subclasses and instances of this class must define the template
property. onUploadComplete(req, res)
method, which is notified when the upload is complete.ArrayUploadAction
An action for uploading an array of files. The uploaded files will be accessible on req.files
.SingleFileUploadAction
An action for uploading a single file. The file is expected to be part of an multipart/form-data request.UploadAction
Base class for all upload actions. This class will initialize a new instance of multer and store it internally for subclasses to use.