0% found this document useful (0 votes)
20 views3 pages

Build REST API with Express.js

This document provides a guide on building a basic REST API using Express.js, covering the setup of Express, creating GET and POST routes, and connecting to a MongoDB database. It explains the fundamentals of REST APIs, including the use of HTTP methods, and includes hands-on tasks for practical implementation. Additionally, it demonstrates how to test the API using Postman and offers examples of real-world REST API applications.

Uploaded by

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

Build REST API with Express.js

This document provides a guide on building a basic REST API using Express.js, covering the setup of Express, creating GET and POST routes, and connecting to a MongoDB database. It explains the fundamentals of REST APIs, including the use of HTTP methods, and includes hands-on tasks for practical implementation. Additionally, it demonstrates how to test the API using Postman and offers examples of real-world REST API applications.

Uploaded by

Rajvi Damrekar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Building a Basic REST API (GET, POST Routes) in Express.

js
Today we discuss how to create a REST API using [Link], handle GET and POST requests,
and implement a simple live project for managing data.

REST API Basics, Setting Up [Link], Creating GET & POST Routes

Connecting API with a Database (MongoDB), Testing with Postman, and Mini Project

API Basics & Creating Routes

1️.What is a REST API?

Understanding REST APIs

• REST (Representational State Transfer) is a set of rules for designing web services.

• Uses HTTP methods:

o GET → Retrieve data

o POST → Send data

o PUT → Update data

o DELETE → Remove data

Real-World Examples of REST APIs:

• Weather API → Fetch weather data (GET)

• E-commerce API → Add items to cart (POST), get products (GET)

Hands-on Task:

1. List 3 APIs students use daily (e.g., Google Maps API, YouTube API).

2️.Setting Up [Link]

Install Required Packages

1. Initialize a [Link] Project:

bash
mkdir rest-api && cd rest-api

npm init -y

2. Install [Link]:

npm install express

3. Create [Link] and Write Basic Express Server:

const express = require("express");

const app = express();

const PORT = 3000;

// Middleware to parse JSON requests

[Link]([Link]());

[Link](PORT, () => {

[Link](`Server running on [Link]

});

Hands-on Task:

1. Run the server:

node [Link]

2. Open Postman or browser and check [Link]

[Link] GET & POST Routes

1️. Create a Simple GET Route

[Link]("/", (req, res) => {

[Link]("Welcome to the REST API!");

});

2️. Create a GET Route to Return Sample Data

const users = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }];


[Link]("/users", (req, res) => {

[Link](users);

});

3. Create a POST Route to Add Data

[Link]("/users", (req, res) => {

const newUser = { id: [Link] + 1, name: [Link] };

[Link](newUser);

[Link](newUser);

});

4. Test Using Postman

• GET [Link]

• POST [Link] (Send { "name": "Charlie" } in JSON body)

You might also like