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, which is the server-side version of JavaScript. There are a lot of other programming languages to choose from, such as Java, C#, 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 structure

13.3.1 Generating the backend

13.3.2 Investigating the structure

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