RESTful Node.js: A Structured Approach
  • Book Cover
  • About the Author
  • Links and Resources
  • Part I: The Why
    • Foreword
    • Preface
    • Chapter 1: Introduction
      • The Rise of REST and Distributed Systems
      • Problem #1: Structureless Design, Structureless REST
      • The Emergence of JavaScript and Node.js
      • Problem #2: Structureless JavaScript, Structureless Node.js
      • Behold, the Solution: A Structured Approach
      • Summary
  • Part 2: The Theory
    • Chapter 2: REST Origins
      • A Brief History of the Web and the Birth of REST
      • REST vs. HTTP
      • REST - The Abstract Web Architecture
      • HTTP - A Peak at REST's Concrete Implementation
      • What does it mean for an API to be RESTful?
      • Measuring "RESTfulness" with Richardson Maturity Model
      • Pragmatic REST vs Dogmatic REST
      • Summary
    • Chapter 3: RESTful API Design Guidelines and "Best Practices"
      • Theories vs. Principles vs. Guidelines
      • URI Design
      • Method Verbs
      • Status Codes
      • Representational Design
      • Metadata Design
      • Versioning Strategies
      • Security Considerations
      • Documentation
      • Case Study: GitHub
      • Summary
    • Chapter 4: Structured JavaScript Architecture
      • The Monstrous Monolith and Its Downfall
      • Layered/N-Tier Architecture: The Unpopular Proven Way
      • Microservices and Distributed Computing: A Popular Misdirection
      • Summary
    • Chapter 5: The 8 Step Recipe
      • Route Name (URI)
      • Input Request
      • Middleware
      • Validation
      • Domain
      • Events
      • Output Response
      • Test, Refactor, Document
      • Summary
  • Part 3: The Code
    • Chapter 6: Introduction to the Bookstore API
      • The Bookstore API Endpoint Specifications
      • API Design and Code Structure
      • Project Setup
      • Summary
    • Chapter 7: Retrieving Books from our API
      • Retrieving All Books - Planning
      • Retrieving All Books - Implementation
      • Retrieving A Book By ID - Planning
      • Retrieving A Book By ID - Implementation
      • Summary
    • Chapter 8: Adding Authentication to our API
      • Registering the User - Planning
      • Registering the User - Implementation
      • Logging the User In - Planning
      • Logging the User In - Implementation
      • Getting Authenticated User - Planning
      • Getting Authenticated User - Implementation
      • Summary
    • Chapter 9: Adding the Create, Update, and Delete Operations to our API
      • Creating A Book Listing - Planning
      • Creating A Book Listing - Implementation
      • Updating A Book Listing By ID - Planning
      • Updating A Book Listing By ID - Implementation
      • Deleting A Book Listing By ID - Planning
      • Deleting A Book Listing By ID - Implementation
      • Summary
    • Chapter 10: Testing our API
      • Testing the Request
      • Testing the Middleware
      • Testing the Validation
      • Testing the Domain
      • Testing the Event
      • Testing the Response
      • Testing the Controller
      • Integration Test
      • Summary
  • Conclusion
    • Final Words
  • Bonus!
    • Refactoring to HATEOAS
  • Appendix
    • Sources & References
Powered by GitBook
On this page
  • 1 - Route Name
  • 2 - Input Request
  • 3 - Middleware
  • 4 - Validation
  • 5 - Domain
  • 6 - Events
  • 7 - Response
Edit on GitHub
  1. Part 3: The Code
  2. Chapter 7: Retrieving Books from our API

Retrieving All Books - Implementation

1 - Route Name

GET /api/v1/books

For the route version prefix, we can do the following.

File: src/server.js

const getRouter = require('./routes')
const router = getRouter()
app.use('/api/v1', router) // add the prefix '/api/v1' to all our URIs

We will then proceed to include all of the book routes.

File: src/routes/index.js

const express = require('express')
const router = express.Router()

const bookRoutes = require('./book.route') // all our book routes

function getRouter() {
  router.use('/books', bookRoutes) // we prefix it here as 'books'
  
  return router
}

module.exports = getRouter

Here, we specify and create a controller method, let's call it getAllBooks.

File: src/routes/book.route.js

const express = require('express')
const router = express.Router()

const {
  getAllBooks
} = require('../controllers/book')

router.get('/', getAllBooks)

module.exports = router

This is where we will put the service layer and the response output.

File: src/controllers/book/getAllBooks.js

const catchException = require('../utils/catchExceptions')

const getAllBooks = catchException(async (req, res, next) => {
  // our code goes here...
})

module.exports = getAllBooks

Now that all the setup is done, we can proceed with the other layers in our application.

2 - Input Request

None.

3 - Middleware

None.

4 - Validation

None.

5 - Domain

The domain layer is quite simple, we want to first create a model that satisfies all our business requirements from the user stories we have in the previous chapter. We are going to create the book model as follows.

File: src/domain/models/book.model.js

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const BookSchema = new mongoose.Schema({
  userId: {
    type: Schema.Types.ObjectId,
    ref: 'user'
  },
  title: {
    type: String,
    required: true
  },
  description: {
    type: String
  },
  price: {
    type: Number,
    required: true
  },
  author: {
    type: String
  },
  datePublished: {
    type: String
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
})

module.exports = mongoose.model('book', BookSchema)

Then, we will wrap it in a repository. Remember why we are using repositories, we want to be able to create an abstraction layer between the concrete implementation of the model and our service layer so that we may swap out database types any time we want. In our case, we are using MongoDB. If for some reason in the future we decide to use something like MySQL, PostgresQL, or OracleDB, it would be much easier to swap out.

File: src/domain/repositories/book.repository.js

const Model = require('../models/book.model')

// Retrieve - all
const getAll = async () => {
  return await Model.find({})
}

module.exports = {
  getAll
}

And finally, our service layer.

File src/domain/services/book.service.js

const bookRepository = require('../repositories/book.repository')
const ApiException = require('../../utils/ApiException')
const mongoose = require('mongoose')

// Retrieve - all books
const getAllBooks = async () => {
  return bookRepository.getAll()
}

module.exports = {
  getAllBooks
}

6 - Events

None.

7 - Response

Here is what we will do to our controller in order to return a list of books

File: src/controllers/book.controller.js

const globalResponseDto = require('../../responses/globalResponseDto')
const booksResponseDto = require('../../responses/booksResponseDto')
const catchException = require('../../utils/catchExceptions')
const bookService = require('../../domain/services/book.service')

/**
 * Gets all book listings from the database.
 */
const getAllBooks = catchException(async (req, res) => {
  const books = await bookService.getAllBooks()

  res.json(
    globalResponseDto({
      status: 'success',
      code: 200,
      message: `List of all books in the database.`,
      data: booksResponseDto(books),
      errors: null
    })
  )
})

module.exports = getAllBooks

File: src/responses/bookResponseDTO.js

function bookResponseDTO(book) {
  return {
    id: book['id'],
    title: book['title'],
    description: book['description'],
    price: book['price'],
    author: book['author'],
    datePublished: book['datePublished']
  }
}

module.exports = bookResponseDTO

File: src/responses/booksResponseDTO.js

const bookResponseDTO = require('./bookResponseDTO')

function booksResponseDTO(books) {
  return books.map((book) => bookResponseDTO(book))
}

module.exports = booksResponseDTO
PreviousRetrieving All Books - PlanningNextRetrieving A Book By ID - Planning

Last updated 3 years ago