Introduction
RESTful APIs (Representational State Transfer Application Programming Interface) plays a vital role in enabling the development of Server-side applications. It allows developers to build web services. Let’s explore the basics of RESTful APIs and how to build them using Node.js.
REST
To begin, REST stands for Representational State Transfer is a software architectural style. It defines a set of constraints(rules) for creating scalable web services.
Web API
In simple terms, Web API (Application Programming Interface) is the way for various software applications to communicate using HTTP-based methods and endpoints. It acts as a bridge to communicate and exchange data over the web.
RESTful API
RESTful APIs is the popular type of web API that uses the principles of REST. It is a simple interface that allows data and resource sharing through web URLs. RESTful APIs are designed to utilize HTTP requests to perform CRUD (Create, Read, Update, Delete) operations on resources.
Setting Up Your Project
To start your Node.js project, create a new directory in your terminal. Then, initialize a new Node.js project using the command, such as:
npm init -y
This command automatically generates a package.json file with default values such as the project name, version, description and more.
Next, install the necessary dependencies:
npm install express body-parser
Creating a Simple RESTful API
Now, let’s create a simple RESTful API with Node.js and Express. Create a new file named server.js in the project directory and add the following code:
const express = require('express'); const bodyParser = require('body-parser'); const app = express(); const PORT = 3000; app.use(bodyParser.json());
app.get(‘/api/files’, (req, res) => { // Logic to fetch all files from the database res.json({ message: ‘GET all files’ }); });
app.post(‘/api/files’, (req, res) => { // Logic to create a new file res.json({ message: ‘Create a new file’ }); });
app.get(‘/api/files/:id’, (req, res) => { const fileId = req.params.id; // Logic to fetch a file by its ID res.json({ message: `GET file with ID ${fileId}` }); });
app.put(‘/api/files/:id’, (req, res) => { const fileId = req.params.id; // Logic to update a file by its ID res.json({ message: `Update file with ID ${fileId}` }); });
app.delete(‘/api/files/:id’, (req, res) => { const fileId = req.params.id; // Logic to delete a file by its ID res.json({ message: `Delete file with ID ${fileId}` }); }); app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });
The above code begins by importing the necessary modules: express and body-parser. Next, creates an Express application by calling express() and stores it in the app variable. Setting Port on which the server will listen for incoming requests.
Use bodyParser.json() as middleware, which parses incoming request bodies in JSON format. Defines several routes using different HTTP methods:
GET /api/files: To fetch all files from the database. POST /api/files: Creating a new file. GET /api/files/:id: Fetching a file by its ID. PUT /api/files/:id: Updating a file by its ID. DELETE /api/files/:id: Delete a file by its ID.
Each route has a callback function that handles the request and sends a JSON response with a corresponding message. Finally, Start the server by calling app.listen(PORT, callback).
Node.js Training in Chennai
To expertise in Node.js, Credo Systemz is the best platform that provides Node.js Training in Chennai. The trainers are real time professionals who offer practical based training. Our Node.js Course in Chennai is placement focused training sessions with hands-on projects.
Conclusion
Finally, This article covered the basics of RESTful APIs and how to build them using Node.js. It helps to understand about setting up a basic Node.js project for Restful API. Get Trained at Credo Systemz Node.js training to build robust APIs!