Designing a REST API with Node.js and MongoDB Atlas

Aman Mittal
3 min readJan 24, 2018

--

Nodejs is a remarkable technology for the fulfilling and developing web applications. Being open source and in the past few years, we have seen it rising. It might be a bit different from other traditional web technologies in terms of learning curve but I urge you to stick with it if you are getting in to web development and have opted Node.js ecosystem. In this tutorial, I will walk you through the basics of REST API design with using Node.js ecosystem.

I’d love if you follow along with me with snippets of code that are in continuation below. Before we begin, I want you to understand what is REST and how its beneficial to use it with server side JavaScript.

What is REST?

REST is an acronym for Representation State Transfer and is an industry standard (till Graphql conquers the world) for defining how an API and its endpoint (routes) should communicate with the server code of your web application. A REST API consumes HTTP methods such GET, POST, etc. with endpoints that are nothing but URLs that you can use to fetch some data or update/create new data in the database. In his whitepaper, Roy Fielding described the paradigms and practical use of REST. I am going to summarise points what he exactly says in that paper. The basis of a RESTful API depends on below:

  • Client-Server
  • Stateless
  • Uniform Interface
  • Cacheable
  • Layered System
  • Code-On-Demand style

To read about them in detail, please see Roy Fielding’s paper on the same here.

Applications that make use of REST APIs perform four basic operations that are known as CRUD.

  • C: Creating data
  • R: Reading data
  • U: Updating data
  • D: Deleting/removing data​

Prequisites

There are some things we need to go through this tutorial successfully.

  • Node.js installed
  • Access to MongoDB, a NoSQL database on your local machine or cloud
  • IDE/Editor
  • Postman, a REST client to test our API’s endpoints

If you do not have MongoDB installed on your local mahcine, do not worry. In the next step I am going walk you through setting up a free tier of MongoDB in cloud.

Consuming MongoDB in Cloud

For the sake of brevity, I am going to use MongoDB Atlas a service that you can use to store data for your sample application. You can also use other popular database-as-a-service Mlab without any hassle.

I already have an account setup, so I’d suggest you to sign up on mongodb atlas’ site with your email. Once you have logged in, you will get the below screen:

At this point, you can either take time to explore what MongoDB Atlas is or you can skip and together we will setup a Sandbox for our sample application. Click on the “New Project” button on the right side of the web page.

Like what you are reading? Please continue to read this article at Zeolearn/blogs on this link here.

If you want me to write an article on web technologies for your publication, please contact me on Twitter! 🙏

--

--

Aman Mittal

👨‍💻Developer 👉 Nodejs, Reactjs, ReactNative | Tech Blogger with 3M+ views at Medium| My blog 👉 https://amanhimself.dev