How to create models and schemas for the API endpoints with Node.js, Express, and Mongoose

This is the second part of the “How to develop API with Node.js and Express” series. Previously we have set up our project. Now we will create models and schemas for the Authors and Books and will create our first API endpoints to create and retrieve authors.

Photo by Glenn Carstens-Peters on Unsplash

Project structure

These are the steps we need to make to create a model for authors and two endpoints we want to develop:

  1. Create a Data Access Object (DAO) Interface that will contain operations we need for the endpoints. Go here to learn more about DAO.
  2. Create a service that has methods needed for endpoints and that would call DAO methods.
  3. Create required endpoints that would call the corresponding service methods.

Models

Run this command to install mongoose:

npm i mongoose

DAO Interface

Create a new file in src/services/authors directory named AuthorsDao.js. Let’s create a class with a method that would create a new Author in the database:

await newAuthor.save()

Authors service

Now we will create a service that will have methods corresponding to methods that we created in DAO. But before doing so we need to create a utility function. We want to store the exact dates users will input for authors birth and death dates, we don’t want them to be dependent on timezone and the dates should have no time (the values for hour, minutes, and seconds would be 0). So let’s create this function that would return exactly that:

src/utils.js file
AuthororsService.js

Router

Finally, we are at the router step. In src/services/authors/AuthorsRouter.js we will create GET and POST endpoints:

AuthorsRouter.js
npm i express-async-handler
npm i body-parser
import mongoose from "mongoose";import * as bodyParser from "body-parser";import { authorsRouter } from "./services/authors";
  • We used body-parser for our server.
  • We added routes from AuthorRouter to the server.
  • We structured the server response when some error happens. Note that we need next parameter for code on line 24, even though we don’t use it
npm run build
{"authors":[]}
{    "author": {        "firstName": "Stephen",        "lastName": "King",        "biography": "Stephen Edwin King (born September 21, 1947) is an American author of horror, supernatural fiction, suspense, crime, science-fiction, and fantasy novels. His books have sold more than 350 million copies, and many have been adapted into films, television series, miniseries, and comic books.",        "dateOfBirth": "09/21/1947"    }}
Screenshot of POST request in Postman
{    "message": "AuthorModel validation failed: firstName: Path `firstName` is required."}

Summary

In this article, we have created schemas and models for our two objects and created two endpoints to create and to retrieve authors. In the next article, we will make proper validation for the data the user sends to the server and create more endpoints.

Full Stack Web Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store