13 Building a Backend with Node.js and Swagger Codegen

 

This chapter covers:

  • Generating backend code with Swagger Codegen
  • Optimizing an OpenAPI definition for code generation
  • Designing a Mongoose/MongoDB database based on the domain model
  • Implementing a basic API operation in Node.js

José’s PetSitter team has created an OpenAPI definition for the API that connects the frontend with the backend. They have now reached the stage of the project where both developers are confident that the API is solid enough to start working on the implementation. In this chapter, we’ll join Nidhi, the backend developer, as she builds a backend that exposes the API that Max designed in chapter 10 and that she herself reviewed in chapter 11. While we’ll touch upon backend functionality like database persistence, our primary focus will be the process of going from API to code.

Our chosen backend technology is Node.js. Node.js is the server-side version of JavaScript. Of course there are a lot of other programming languages to choose from, such as Python, Ruby, Go (which we used for the FarmStall API in part 1), or PHP. We picked JavaScript because we expect most developers to have at least a basic grasp of the language syntax, even if it’s just from the client-side.

13.1 The problem

 
 

13.2 Introducing Swagger Codegen

 

13.2.1 Client code generation

 
 
 
 

13.2.2 Server code generation

 

13.2.3 Swagger Generator

 

13.3 The backend architecture

 
 

13.3.1 Generating the backend

 
 
 

13.3.2 Investigating the architecture

 

13.3.3 OpenAPI changes

 

13.4 Updating OpenAPI for the backend

 

13.4.1 Adding operation IDs

 
 

13.4.2 Tagging API operations

 
 
 

13.4.3 Regenerating the backend stubs

 
 

13.5 Running and testing the backend

 
 
 
 

13.5.1 Testing with Postman

 
 
 

13.5.2 Testing input validation

 
 

13.5.3 Output validation with prism

 
 
 

13.6 Database persistence with Mongoose

 
 

13.6.1 Another API modification

 
 

13.6.2 Getting ready to use MongoDB

 
 

13.6.3 Configuring Mongoose in the project

 
 
 
 

13.6.4 Creating models

 
 
 

13.7 Implementing API methods

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest