Build an API using Node, Express, MongoDB, and Docker

Posted on

In this tutorial, we’ll be using TypeScript and Docker to build a Menu Restaurant API app from scratch with Node, Express, and MongoDB. The Docker part is optional.

Basically, we should be able to:

  • retrieve all menus
  • retrieve one menu
  • create a menu
  • update a menu
  • delete a menu

Great, let’s dive in.

Setting up

To create a new Nodejs project, we’ll first run this command on the terminal.

yarn init
Enter fullscreen mode

Exit fullscreen mode

It’ll ask a couple of questions before initializing the project. Anyway, you can bypass this by adding a -y flag to the command.

Next step is to create a structure for our project.

├── dist
├── src
   ├── app.ts
   ├── controllers
   |  └── menus
   |     └── index.ts
   ├── models
   |  └── menu.ts
   ├── routes
   |  └── index.ts
   └── types
      └── menu.ts
├── nodemon.json
├── package.json
├── tsconfig.json
Enter fullscreen mode

Exit fullscreen mode

Let me quickly explain the structure of the project.

  • dist will serve as the output folder once the typescript code is compiled to plain JavaScript.
  • src will contains the logic of our API.

    • app.ts is the entry point of the server.
    • controllers will contain functions that handle requests and return data from the model to the client
    • models will contain objects that will allow basic manipulations with our database.
  • routes are used to forward the requests to the appropriate controller.
  • types will contain the interface of our objects in this project.

To continue, let’s add some configurations to tsconfig.json. This will help the computer along following our preferences for development.

  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist/js",
    "rootDir": "src",
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  "include": ["src/**/*"],
  "exclude": ["src/types/*.ts", "node_modules", ".vscode", ".idea"]
Enter fullscreen mode

Exit fullscreen mode

Now we can start installing the dependencies to start our project. But first, let’s enable TypeScript.

yarn add typescript
Enter fullscreen mode

Exit fullscreen mode

Let’s also add some dependencies to use Express and MongoDB.

yarn add express cors mongoose
Enter fullscreen mode

Exit fullscreen mode

Next, we’ll be adding their types as development dependencies. This will help the TypeScript computer understanding the packages.

yarn add -D @types/node @types/express @types/mongoose @types/cors
Enter fullscreen mode

Exit fullscreen mode

Let’s add some dependencies for auto-reloading the server when a file is modified and start the server concurrently (We’ll be able to make changes and start the server simultaneously).

yarn add -D concurrently nodemon
Enter fullscreen mode

Exit fullscreen mode

We need to update the package.json file with the scripts needed to start the server and build the project.
Here’s how your package.json file should look.

"name": "menu-node-api",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"mongoose": "^6.0.11",
"nodemon": "^2.0.13",
"typescript": "^4.4.4"
"scripts": {
"build": "tsc",
"start": "concurrently "tsc -w"" ""nodemon dist/js/app.js""""

Leave a Reply

Your email address will not be published. Required fields are marked *