0% found this document useful (0 votes)
3 views

RestAPI

REST API (REpresentational State Transfer API) facilitates communication between systems over the internet using HTTP methods for resource manipulation. Key features include being stateless, following a client-server architecture, and having a uniform interface. Applications span various domains such as social media integration, e-commerce, geolocation services, and weather forecasting, with a sample Express.js implementation provided for creating a RESTful API.

Uploaded by

clawpew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

RestAPI

REST API (REpresentational State Transfer API) facilitates communication between systems over the internet using HTTP methods for resource manipulation. Key features include being stateless, following a client-server architecture, and having a uniform interface. Applications span various domains such as social media integration, e-commerce, geolocation services, and weather forecasting, with a sample Express.js implementation provided for creating a RESTful API.

Uploaded by

clawpew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Notes:

REST API stands for REpresentational State Transfer API. It is a type of API (Application
Programming Interface) that allows communication between different systems over the
internet.
REST APIs work by sending requests and receiving responses, typically in JSON format,
between the client and server.

REST APIs use HTTP methods (such as GET, POST, PUT, DELETE) to define actions that can
be performed on resources. These methods align with CRUD (Create, R=ead, Update, Delete)
operations, which are used to manipulate resources over the web.
Key Features of REST APIs

● Stateless: Each request from a client to a server must contain all the information the
server needs to fulfill the request. No session state is stored on the server.

● Client-Server Architecture: RESTful APIs are based on a client-server model,


where the client and server operate independently, allowing scalability.

● Cacheable: Responses from the server can be explicitly marked as cacheable or non-
cacheable to improve performance.

● Uniform Interface: REST APIs follow a set of conventions and constraints, such as
consistent URL paths, standardized HTTP methods, and status codes, to ensure
smooth communication.

● Layered System: REST APIs can be deployed on multiple layers, which helps with
scalability and security.
Applications of REST APIs

● Social Media: Integrating third-party social media platforms.

● E-Commerce: Processing payments, managing products, etc.

● Geolocation Services: GPS tracking, location-based services.

● Weather Forecasting: Fetching weather data from external sources.


// Import the Express module
const express = require('express');
const app = express();
const port = 3000;

app.use(express.json());

// Define a route for GET requests


app.get('/users', (req, res) => {
res.json({ message: 'Returning list of users' });
});

// Define a route for POST requests


app.post('/users', (req, res) => {
const newUser = req.body;
res.json({ message: 'User created', user: newUser });
});

// Define a route for PUT requests


app.put('/users/:id', (req, res) => {
const userId = req.params.id;
const updatedUser = req.body;
res.json({ message: `User with ID ${userId} updated`, updatedUser });
});

// Define a route for DELETE requests


app.delete('/users/:id', (req, res) => {
const userId = req.params.id;
res.json({ message: `User with ID ${userId} deleted` });
});

// Start the server


app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Steps
1. Open vs code and create a new folder
2. Open terminal and type npm init -y
3. On terminal type npm i express
4. Create index.js file which is heart of your backend
5. Add "serve": "node index.js" in package.json file
6. Install nodemon nodemon is a tool that helps develop Node.js based applications by
automatically restarting the node application when file changes in the directory are
detected.

Sample
)https://www.mongodb.com/resources/languages/express-mongodb-rest-api-tutorial

You might also like