8 Steps to develop RESTful API using Express and MongoDB

By | February 16, 2017

In this MEAN Stack Tutorial, we are going to develop a RESTful API using Express.js and MongoDB. We will follow a step by step approach to make it easier for readers in developing their first RESTful API on MEAN stack.

Following are the steps to build the API:

Step 1: Initialize the Project

  • Create a folder with the project name such as todoapp.
  • Open command prompt window on the project folder.
  • Run the following command:
    This command will create a package.json file with following details:

    MEAN Stack Tutorial

Step 2: Install Express

  • Install express with following command:

Step 3: Install Express Generator

  • Install express-generator using following command

Step 4: Create the Project

  • Use the following command to create the express project

    This will give the following output

Step 5: Install Dependencies

  • The package.json file will look like as following:

  • Install all the dependencies using following command:

Note: You can find more helpful details on MEAN Stack especially AngularJS 1.x & AngularJS 2 here.

Step 6: Install HBS, MONGO

  • Install mongo  using following commands:

Step 7: Run Mongo

  • Run mongod.exe it will start the MongoDB.Run MongoDB
  • Connect to todoapp databse using following command:Mongo Exe

Step 8: Create Mongo Service and Connect to Database

  • On app.js file add mongo database as following:
  • Create a middleware to connect to database
  • Create a new router for todo in ‘routes/todo.js’ file
  • In app.js add the router for initialization
  • Create a new form template inside views folder with the name ‘new_todo.jed’
  • Run the app using the following command
  • From browser if we run http://localhost:3000/todos/new, we can view the New todo page as following:ExpressJS Tutorial
  • Next step is the write the method that will be executed once the add-todo form is submitted in routes/todos.js file add the following router method.
  • Now submitting the form will redirect to /todos/add-todo route and make a POST request. The todo will be saved in the database.

Hopefully, this MEAN Stack tutorial will serve as your first RESTful API using ExpressJS with MongoDB. We will come with more detailed technical tutorial on related technologies soon. Keep in touch 🙂

Top Related Technical Articles: