Create REST API with Node and Express - Part 4

We are creating a REST API for a simple blog post that will serve blog posts and author information.

author profile image
Sakchhyam G
@sakchhyam

Share

Create REST API with Node and Express - Part 4

Table of content

  1. What is a controller layer?
  2. Code
  3. Summary

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.

Code

  • In your Node.js project, navigate to the src folder and create a controllers.js file inside both the author and the post folder.

  • The Node.js project structure will look something like this:

    1src/ 2├─ author/ 3│ ├─ schema.js 4│ ├─ services.js 5│ ├─ controllers.js 6├─ post/ 7│ ├─ schema.js 8│ ├─ services.js 9│ ├─ controllers.js 10├─ config.js 11.env 12package.json 13server.js

author controllers

  • We will import the author services 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.

  • In the ./src/author/controllers.js file put the below code.

    1// import author's services 2const AuthorServices = require("./services"); 3 4// the controller receives three main parameters from our express app 5// req: the client's request 6// res: the response object to send back the client's response 7// next: passes controls to next inline middleware or handler 8async function createAuthor(req, res, next) { 9 // body is the standard object available in the HTTP request object. "data" is the actual data object sent by the client 10 const { data } = req.body; 11 // controller can use one or multiple services to complete the intended job 12 const result = await AuthorServices.createAuthor(data); 13 // sending back author information with a status code of 200 (indicates successful request) 14 res.status(200).send({ author: result }); 15} 16 17async function getAuthorById(req, res, next) { 18 // the params object holds URL parameter data 19 // eg: if URL for getAuthorById() is defined as https:localhost:5000/api/v1/authors/:id then :id represents URL param data 20 const { id } = req.params; 21 const result = await AuthorServices.getAuthorById(id); 22 res.status(200).send({ author: result }); 23} 24 25async function getAllAuthor(req, res, next) { 26 const result = await AuthorServices.getAllAuthor(); 27 res.status(200).send({ authors: result }); 28} 29 30module.exports = { createAuthor, getAuthorById, getAllAuthor };

post controllers

  • We will repeat the same steps with post controllers.

  • In the ./src/post/controllers.js file put the below code.

    1const PostServices = require("./services"); 2 3async function createPost(req, res, next) { 4 const { data } = req.body; 5 const result = await PostServices.createPost(data); 6 res.status(200).send({ post: result }); 7} 8 9async function getPostById(req, res, next) { 10 const { id } = req.params; 11 const result = await PostServices.getPostById(id); 12 res.status(200).send({ post: result }); 13} 14 15async function getAllPost(req, res, next) { 16 const result = await PostServices.getAllPost(); 17 res.status(200).send({ posts: result }); 18} 19 20async function updatePostById(req, res, next) { 21 const { id } = req.params; 22 const { data } = req.body; 23 const result = await PostServices.updatePostById(id, data); 24 res.status(200).send({ posts: result }); 25} 26 27module.exports = { createPost, getPostById, getAllPost, updatePostById };

Summary

  • We have created controllers for the author and the post.
  • In the next part, we will specify routes to our controller layer.
  • The link to the project repo is available here.
Advertisement
Advertisement