Gabriele Ferro Home

Created a REST API with Node, Express and MongoDB

Yesterday, I finished the Codecademy JavaScript course and now I want to complete freeCodeCamp and OpenClassRooms courses.

Regarding OpenClassRooms, I have 3 courses to study and write the code of 2 projects.

I have to start writing the code of the sixth project, but in order to do this, I will first have to finish the course about RESTful API and then I will have to learn the OWASP techniques with the other course.

Concerning freeCodecamp, I have to code 6 algorithms and 2 projects to complete the JavaScript Algorithms and Data Structure Certification.

Day 74: 2 November 2020

**Today’s Progress:**


The first challenge was the Quiz:

Do you know how to implement the CRUD?

Yes, I do.

The secret word shows up at the end of the Quiz

In this Quiz we have a practical case and not questions & answers: create an API that manages products in a database. So it's about writing the middlewares to manage each route, that is to write the javascript code that manages the Creation, Read, Update, Delete of a database entry.

Unfortunately, I still don't have enough self-confidence to be able to write server-side JavaScript without having doubts about the syntax or memory lapses.

Me trying to remember the code without peeking

Luckily I have this blog, so I can leave some notes and pieces of code that will help me in the future.

So, let's do this!

First step is: We clone the frontend and create the structure of our project.

We'll have two folders:

  1. the frontend folder that contains the git repository we just cloned;
  2. the backend folder where we'll write our code in the next steps.

Frontend: From the cloned folder, we run:

npm install 

and then

npm start.


Inside our folder we initialize the project writing on terminal

npm init

It has always seemed to me a very obscure and complicated thing, but in reality it is nothing more than "create the package.json file in this folder".

By launching npm init we just create the file 'package.json' with the desired parameters.

As entry point we will choose server.js instead of index.js

We can also globally install the nodemon module. This package will allow us not to have to write 'node server' in the terminal every time we change.

npm install -g nodemon

It will be easier to launch the command only once:

nodemon server

We add Express to our project:

npm install --save express

To be able to extract JSON, we add the body-parser module to our project:

npm install --save body-parser

In order to interact in an extremely simple way with MongoDB, we also add the mongoose module:

npm install --save mongoose

At this point, we can also initialize a Git repository by running

git init

from our backend folder.

It is very important not to forget to create the .gitignore file that will contain the line of code:


Once we have installed all the necessary modules, we create a model (in this case a Product) containing the format to use:

Then, we write the code for our server.js

And we finally code the routes for our backend:

We can use Postman to test our API.


Fill in the object constructor with the following methods below:


Run the tests to see the expected output for each method. The methods that take an argument must accept only one argument and it has to be a string. These methods must be the only available means of interacting with the object.

Intermediate Algorithm Scripting Challenge: Make a Person

At the beginning of the challenge I didn't notice that one of the tests to pass was that the object must contain exactly SIX keys.

So it means that only the methods in question will have to appear as properties of our object.

Return a new array that transforms the elements' average altitude into their orbital periods (in seconds).

The array will contain objects in the format {name: 'name', avgAlt: avgAlt}.

You can read about orbital periods on Wikipedia.

The values should be rounded to the nearest whole number. The body being orbited is Earth.

The radius of the earth is 6367.4447 kilometers, and the GM value of earth is 398600.4418 km3s-2.

Intermediate Algorithm Scripting Challenge: Map the Debris


The day started with 0 self-esteem.

I must avoid any kind of distraction.

I have to be more confident.

But more important:

Remind yourself that overconfidence is a slow and insidious killer.

Darkest Dungeon

**Link to tweet:**

[twitter-follow screen_name='20Gabriele20']

February 2024


Gabriele Ferro Home
Copyright © Ferro Gabriele. All rights reserved.