Skip to content

Akhila Ariyachandra

Note App - Part 1: Setup the Node API

Node, MongoDB, Mongoose, Note App2 min read

In this series we're going to create a notes app with Node, MongoDB & React. For the first part, let's setup the API.

I'm going to be using yarn as my package manager, but feel free to use npm or pnpm instead.

Initialize the project

First let's create a folder called 'notes-api' and initialize a project in it using

We're going to use Backpack to build and run our API, so we'll install it as a dev dependency. Backpack has useful features like automatically reloading the server when any of the files are changed and being able to run ES6 code with without any configuration.

Then add the following scripts to package.json.

yarn dev is used to start the development server and yarn build is used to get the production build.

Setup the database

Next let's setup the MongoDB database. If you want a more detailed explanation on setting up the database, you can check out my previous post Setup MongoDB in Node.js with Mongoose.

We'll create a database with mLab. Once you create it, you should create an user for the database and keep note of it's path. The path should be something like mongodb://<dbuser>:<dbpassword>@ds250607.mlab.com:38485/notes-db where <dbuser> and <dbpassword> are the username and password of the user you created for the database respectively.

Then install the mongoose dependency.

After that create a folder with the name 'src' in the project,create a file named database.js and insert the following code in it.

Replace the value of dbPath with the path of your database.

Create the notes model

Once we're done setting up the database, we need to create a model to perform CRUD (Create, Retrieve, Update & Delete) operations on the database.

First create a folder with the name 'models' in 'src'. Then create a file called noteModel.js in it and add the following code.

Setup the server and routes

Now that we have setup the database and user model, we can start creating the server and the routes for the API. We'll be creating the server with Express.

Let's install the dependencies

After the dependencies have finished installing create the file index.js in 'src' with the code

Just to make sure what we've done so far works fine run yarn dev. If nothing's wrong it should show

Let's stop the server with Ctrl+C and continue adding the routes in the API.

Our first route will be one to create new notes.

The next route will be to get the entire list of notes

We'll need a route to get a single note based on the ID.

The second to last route will be to update a note.

Our final route will be to delete a note.

Wrapping up

I deliberately left out testing the API as I didn't want to make this post too long but you could use software like Postman or Insomnia to test the API. All the code from this post is available on GitHub. The next part will be about creating a front-end with React to use with this API.