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