Create REST API with Node and Express - Part 3

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

author profile image
Sakchhyam G


Create REST API with Node and Express - Part 3

Table of content

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

What is a service layer?

  • The service layer is where we hold our business logic.
  • This layer receives data and tasks from the controller layer. It performs its specific task and returns the processed data to the controller.
  • The service layer uses a data model to access database functionality and perform an operation on the database.
  • It doesn't handle HTTP requests and responses directly.


  • In your Node.js project, navigate to the src folder and create a services.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├─ post/ 6│ ├─ schema.js 7│ ├─ services.js 8├─ config.js 9.env 10package.json 11server.js

author services

  • We will import the Author model and use provided functions to perform various operations on the author's documents.

  • The functions created below hold the logic to complete a particular operation or task.

  • Each function uses a model instance to perform database operations.

  • Each function can receive data from the respective controller.

  • After completing the task, each function returns processed data to controller.

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

    1const Author = require("./schema"); 2 3async function createAuthor(authorData) { 4 // authorData is provided by the controller 5 // creating an author document with field data as a parameter 6 const doc = new Author(authorData); 7 // using save() to insert document in database 8 // returns promises so either use asyn-await or then-catch to handle resolve or reject. 9 const result = await; 10 return result; 11} 12 13async function getAuthorById(authorId) { 14 // using findById() provided by Author model 15 // supplying author's unique id or ObjectId to extract the author's document 16 const result = await Author.findById(authorId); 17 return result; 18} 19 20async function getAllAuthor() { 21 // using find() to extract all the author documents (array) 22 const result = await Author.find(); 23 return result; 24} 25 26// exporting services to be used in controllers 27module.exports = { createAuthor, getAuthorById, getAllAuthor };

post services

  • We will repeat the same steps for post services.

  • populate(): Since we have structured our post schema to hold the author's reference. We can use the populate() function to fill the authorRef field with author's information. In this case, we are only interested in the author's name. We can use a comma-separated string to include other fields from the author.

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

    1const Post = require("./schema"); 2 3async function createPost(postData) { 4 const doc = new Post(postData); 5 const result = await; 6 return result; 7} 8 9async function getPostById(postId) { 10 // populate() fills authorRef field with author's name 11 const result = await Post.findById(postId).populate("authorRef", "name"); 12 return result; 13} 14 15async function getAllPost() { 16 const result = await Post.find().populate("authorRef", "name"); 17 return result; 18} 19 20async function updatePostById(postId, data) { 21 // controller suppies post's unique id (objectId) and updated data 22 // { new: true } option makes function to return document with updated data 23 const result = await Post.findByIdAndUpdate(postId, data, { new: true }).populate( 24 "authorRef", 25 "name" 26 ); 27 return result; 28} 29 30module.exports = { createPost, getPostById, getAllPost, updatePostById };


  • We have created schemas for the author and the post.
  • In the next part, we will create the controller for each collection.
  • The link to the project repo is available here.