In  the first article of our REST API series, we covered how to use npm to create a back end from scratch, add dependencies like  TypeScript, use the debug module built into Node.js, build an Express.js project structure, and log runtime events flexibly with Winston. If you’re comfortable with those concepts already, simply clone  this, switch to the toptal-article-01 branch with git checkout, and read on.

REST API Services, Middleware, Controllers, and Models

As promised, we’ll now get into details about these modules:

  • Services that make our code cleaner by encapsulating business logic operations into functions that middleware and controllers can call.
  • Middleware that will validate prerequisite conditions before Express.js calls the appropriate controller function.
  • Controllers that use services to process the request before finally sending a response to the requester.
  • Models that describe our data and aid in compile-time checks.

We will also add a very rudimentary database that is in no way suitable for production. (Its only purpose is to make this tutorial easier to follow, paving the way for our next article to delve into database connection and integration with MongoDB and Mongoose.)

Hands-on: First Steps with DAOs, DTOs, and Our Temporary Database

For this part of our tutorial, our database won’t even use files. It will simply keep user data in an array, which means the data evaporates whenever we quit Node.js. It will support only the most basic create, read, update, and delete (CRUD) operations.

We are going to use two concepts here:

  • Data access objects (DAOs)
  • Data transfer objects (DTOs)

#typescript #node.js

Node.js REST API Tutorial: Architecture, DTOs, and TS
6.25 GEEK