EventEmitter Class
An event emitter is a pattern for making different parts of a program talk to each other by sending and receiving messages. It's a way to create responsive and interactive applications. The Server, and Router class extends from this class. The following code is an example of creating an emitter, a very basic listener and emitting the event.
❐ Copy
Where as is the name of the event and is the action function that will be executed when the event is emitted.
With Server
In relation to the server, you can listen to events in the following way.
❐ Copy
With Routing
To explain the problem, consider the following route.
❐ Copy
The example above describes a route with several steps involved and overtime can lead to issues. Setting up an event driven design where an events are designed as units of function could manage the technical debt later down the line. Consider the following usage of events with routing.
❐ Copy
The example above shows how to create a home page route that emits several events. The event is used to log the view in the database and the event is used to send a response to the client. This way, you can separate the concerns of logging and responding from the route.
Event Names
A basic event name is just a string. The following code is a very basic example of an event that will listen to the event name as is.
❐ Copy
Event names can also be formed as patterns to represent dynamic variable pathing. The following code is an example of an event name with a named variable called .
❐ Copy
The above example shows how the app listens to the event name. This means the following path examples are accepted.
- •
- •
Where as would be , or . The parameter is a variable that can be extracted using the method. Another pattern called a wildcard can be used to match any single path segment.
❐ Copy
The above code is an example of an event that listens to the event name. Unlike variable paths, the wildcard matches are nameless and thus denoted by numbers. This means the following event names examples will trigger.
- •
- •
Where as would be , or . A more powerful pattern than the is the rest pattern that matches any number of path segments.
❐ Copy
The above code is an example of a route path that listens to the path. In this example, any names that start with will be triggered. This means the following path examples are triggered (but not limited to).
- •
- •
Lastly an event name can also be a regular expression. The following code is an example of an event name that listens to the event expression.
❐ Copy
Event Priority
An event priority is a number that determines the order in which the event actions are executed. The higher the number, the higher the priority. The following code is an example of three events with different priorities.
❐ Copy
The above code is an example of three events with different priorities. The first event has a priority of , the second event has a priority of , and the third event has a priority of . This means the first event will be executed first, followed by the second event, and finally the third event. The default priority is .
Event Actions
An event action is a function that is executed when the route is matched. The following shows an example of a basic route action. When an event is emitted from the server the , and are the Request, and Response, and Server objects, respectively.
❐ Copy
The simplicity of the above code is what made this kind of pattern popular, but it comes with a few issues.
- • The actions are defined, but not necessarily used. Consider a serverless environment.
- • No event mapping for bundlers. Cannot do tree shaking or code splitting for example.
- • Cannot be used effectively with a module loader. The action is not a module, and thus cannot be loaded.
NOTE: Basically the same issues as route actions.
Because of the reasons above, the method can also accept a function that returns an import. The following code is an example of an event action that returns a module.
❐ Copy
The above code is an example of an event action that returns a module. The module is lazily loaded when the route is matched. Modules imported this way need to a function that accepts, and arguments.
❐ Copy
An easier way to create an event action is to use the function like the following code.
❐ Copy