RESTFUL API
REST stands for Representational State Transfer. It is web standards architecture and HTTP Protocol. REST uses various resources like text, JSON, XML out of which JSON is the most popular one.
RESTFUL applications use HTTP request to perform four operations known as CRUD(where C: Create, R: Read, U: Update and D: Delete).
RESTful is composed of methods such as; base URL, URL, media types, etc.
In this tutorial, we will learn how to create a RESTful API using Node.js
TOOLS:
- js
- MongoDB
- Express(Run the following command: npm install express --save)
- Text editor (Atom, Sublime, Visual Studio Code etc.)
Let’s create REST API :
step1: Install mongodb(Database)
step2: Install mongoose(npm install --save mongoose)
step3: Create project folder--> naming your project--> double click at project name(Nodefirstproject)--> open will be toolbar--> click --> then type cmd --> enter--> open will be command prompt--> type "npm init". This wizard will ask you some questions like name,version, test script etc. and the end you will have your package.json file ready.
After creating package.json run command npm install
Setting up the server:
To set up our project Server, we are going to use the Express module. Here shown is our basic server.
Let's install express and nodemon, express here will be used to create the server while nodemon will help us to keep track of changes to our application by watching changed files and automatically restart the server.
Run the following Code: open terminal (Ctrl +backtick) (Ctrl+`)
This should be display
Here rest Api project is my project name.
Creating basic MongoDB model (Setting up the schema).
First, install mongoose - npm install mongoose -- save
Mongoose allows you to create models (or Schema or tables) in your Mongodb database to connect mongoDB database to our Node.js
demoDb is the database's name MongoDB uses JSON structure to create schemain model. So for example, if you want to create simple table Login_table with columns say email, a password you need to write following code.
Create separate folder (in your project) named Config--> Model --> in inside model create file name --> named(mongo.js) in mongo.js
Explanation of above example step by step:
- Create mongoose module (in the first line).
- connect to Database(demoDb is my Database name)
- Create an instance of Schema.
- Create scheme(email, password) this field should be same in the database.
- Here "users" is my collection name.
Now, in index.js add following code
var mongoOp = require(" ./model/mongo");
Now we should start by making API. our resource will be "users" and on that, we are going to allow CRUD operations.
- GET /users – Return all Users from MongoDB
- POST /users – Add a new user in MongoDB
- PUT /users/:id – Update users information
- DELETE /users/: id – Delete particular user
1: GET /users – Return all Users from MongoDB.
In Index.js:
Run the above code: http://localhost:3000/users
Your data will be display, which you have taken in the database
If you have to Test API, Open Rest Easy--> Hit url --> http://localhost:3000/users , Method --> GET --> Headers -->select content-type, value--> application then click SEND
2: POST /users – Add a new user in MongoDB.
Then open the Rest Easy. Hit the url--> http://localhost:3000/postUsers then open will be this page