Silvie HTTP has been built on MVC structure. So you need to create controllers in order to respond to user requests.
A controller is just an inherited class from the base
Controller class. The base Controller class has no default
behavior by itself. It is just used to detect controllers later at runtime.
You can create a new controller with Silvie CLI. This command will create a controller named
ProductsController in the
src/controllers directory of your project.
A basic controller will look like this:
It is a default exported class inherited from
Controller. You need to create route handlers in the class body.
Controllers need to load at runtime. You need to load them all in the project bootstrap file, before calling the core bootstrap function.
This type of import utilizes babel-plugin-wildcard-import to import all files in a directory. Just importing the controllers will cause the method decorators to execute and register the route handlers in the application.
A route handler is a non-static method defined in a controller class, since they will be used as callback methods, they have two parameters which will be passed by the express server.
The example above, will send a
400 status code if the connection was not a TLS connection, and might send a
status code when it fails to fetch posts from the database. Otherwise, it will send all posts to the user.