Table of content
What is a controller layer?
- The controller layer is where we handle the incoming HTTP requests and outgoing responses.
- This layer uses the services from the service layer.
- After completing the job, this layer constructs responses with appropriate status codes and data.
- This layer can take help from middlewares for tasks like authenticating client's request, securing the routes, parsing request body, etc. Middlewares are the extra layer of functions that have access to request and response objects from the HTTP calls.
In your Node.js project, navigate to the
srcfolder and create a
controllers.jsfile inside both the
The Node.js project structure will look something like this:
We will import the
authorservices and use appropriate functions to handle the requested task.
We will extract the request body from the client.
We will also extract the parameters from the URL, if any.
After completing the requested task, we will usually respond with processed data with a status code.
./src/author/controllers.jsfile put the below code.
We will repeat the same steps with
./src/post/controllers.jsfile put the below code.
- We have created controllers for the
- In the next part, we will specify routes to our controller layer.
- The link to the project repo is available here.