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 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.
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 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
- 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.
- Now create a file called
server.jsin your project directory and import express inside it.
- Now create an application called ‘app’, that inherit all features of express js, by adding the following code in the
- 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
- 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
- Lastly, we must connect to mongodb using mongoose wrapper, so we need some lines of code in
Create simple HTTP Requests (get, post, update and delete):
Finally, server.js file must be something like this
=> 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.
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.