Build Simple REST API With Node.js, Express And Mongoose

Prerequisites:

These software need to be installed on your machine first:

What is REST API?

A RESTful API, also known as REST API, is based on Representational State Transfer (REST), which is an architectural style and communication approach commonly used in the development of Web Services.

RESTful APIs can be designed using programming languages like JavaScript or Python.

RESTful API is an architectural style for an application program interface (API) that uses HTTP requests to access and use data. That data can be used to GET, PUT, POST and DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources.

Node.js:

Node.js is an Open-Source Server-Side Runtime Environment that is solely based on the V8 JavaScript Chrome Engine.

Node.js can be used to create a variety of applications, including Command-Line Applications, Web Applications, Real-time Chat Applications, REST API Servers, and so on. Its Event-Driven Runtime handles all types of HTTP requests and sleeps when not required. This allows developers to leverage JavaScript and write Server-Side scripts to generate Dynamic Web Content before it is delivered to the user’s web browser.

Express.js:

Express is a fast, assertive, essential and moderate web framework of Node.js. You can assume express as a layer built on the top of the Node.js that helps manage a server and routes. It provides a robust set of features to develop web and mobile applications.

  • It can be used to design single-page, multi-page and hybrid web applications.
  • It allows to setup middlewares to respond to HTTP Requests.
  • It defines a routing table which is used to perform different actions based on HTTP method and URL.
  • It allows to dynamically render HTML Pages based on passing arguments to templates.

Mongoose:

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB. MongoDB is a schema-less NoSQL document database.

Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. Mongoose supports both promises and callbacks.

Build Simple REST API With Node.js, Express And Mongoose

Let’s start!

  • Create a new project folder and then run the following command in Command Line:

npm init -y

=> (-y flag) create packages.json with default options

  • Install the dependencies listed above just like so:

npm install express mongoose

=> Check your packages.json if all dependencies are installed or not.

package.json
  • Now create a file called server.js in your project directory and import express inside it.
Importing Express js
  • Now create an application called ‘app’, that inherit all features of express js, by adding the following code in the server.js file:
Inheritance
  • We need to introduce a middleware so our server recognizes the incoming request objects as JSON objects. For that, add the following piece of code in server.js file:
a middleware like body parser
  • Now, we need to listen for a connection to know that our server is running. You can do this by adding the following lines of code in your server.js file:
run the server on $port
  • Lastly, we must connect to mongodb using mongoose wrapper, so we need some lines of code in server.js file:
connect node.js with mongodb

Create simple HTTP Requests (get, post, update and delete):

HTTP Requests

Finally, server.js file must be something like this

server.js

=> Postman is a great tool when trying to dissect RESTful APIs made by others or test ones you have made yourself. It offers a sleek user interface with which to make HTML requests, without the hassle of writing a bunch of code just to test an API’s functionality.

Conclusion

This may be the biggest reason which makes a Restful API or REST API much simple and easy to use. Such APIs are used in day-to-day modern web apps as they render content fast and are also not that handy in coding.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store