0% found this document useful (0 votes)
6 views8 pages

Program 16

The document outlines the steps to develop an Express web application for performing CRUD operations on student data using a REST API and Postman. It includes instructions for installing Postman, initializing a Node.js project, setting up the server with Express, and defining routes for handling student data. Additionally, it provides guidance on how to test the API endpoints using Postman for GET, POST, PUT, and DELETE requests.

Uploaded by

vinayrock119
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)
6 views8 pages

Program 16

The document outlines the steps to develop an Express web application for performing CRUD operations on student data using a REST API and Postman. It includes instructions for installing Postman, initializing a Node.js project, setting up the server with Express, and defining routes for handling student data. Additionally, it provides guidance on how to test the API endpoints using Postman for GET, POST, PUT, and DELETE requests.

Uploaded by

vinayrock119
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/ 8

PROGRAM 16

AIM: - Develop an express Web application that can interact with REST API to perform
CRUD operations on student data. (Use Postman)

How to Install Postman Software:-


 sudo apt update
 apt search package_name
 sudo apt install package_name
 sudo snap install postman
 postman
 sudo snap refresh postman

Step1:-

1) Initialize the Project: Create a new directory for your project and initialize npm.

mkdir student-api-app
cd student-api-app
npm init -y

2) Install Dependencies: Install necessary packages such as Express and other middleware.

npm install express body-parser axios

Step 2: Create the Server and Basic Routes


Create an app.js file where you'll define your Express server and routes.

// app.js
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');

const app = express();


const PORT = process.env.PORT || 3000;

// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Routes
app.get('/', (req, res) => {
res.send('Welcome to the Student API App');
});

// Example API routes will go here

// Start server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
// app.js continued...

// Example data (can be replaced with a database connection)


let students = [
{ id: 1, name: 'Varma', age: 20 },
{ id: 2, name: 'Venkat', age: 21 },
];

// Get all students


app.get('/students', (req, res) => {
res.json(students);
});

// Get a single student by ID


app.get('/students/:id', (req, res) => {
const student = students.find(s => s.id === parseInt(req.params.id));
if (!student) {
return res.status(404).json({ message: 'Student not found' });
}
res.json(student);
});

// Create a new student


app.post('/students', (req, res) => {
const { name, age } = req.body;
const id = students.length + 1;
const newStudent = { id, name, age };
students.push(newStudent);
res.status(201).json(newStudent);
});

// Update a student by ID
app.put('/students/:id', (req, res) => {
const id = parseInt(req.params.id);
const { name, age } = req.body;
const student = students.find(s => s.id === id);
if (!student) {
return res.status(404).json({ message: 'Student not found' });
}
student.name = name;
student.age = age;
res.json(student);
});

// Delete a student by ID
app.delete('/students/:id', (req, res) => {
const id = parseInt(req.params.id);
students = students.filter(s => s.id !== id);
res.status(204).end();
});

// End of CRUD operations

Step 3:
Start the Server: Run the Node.js application using the node command followed by the name of
your main JavaScript file (in this case, app.js).

node app.js

Step 4:
Verify Server is Running: After running the command, you should see a message in the terminal
indicating that the server is running, similar to:

Server is running on http://localhost:3000

Output :

GET:
 Open Postman.
 Set the request type to GET.
 Enter the URL: http://localhost:3000/students.

POST : Create a New Student
 Open Postman.
 Set the request type to POST.
 Enter the URL: http://localhost:3000/students.
 Go to the "Body" tab.
 Select raw and set the body to JSON format.

GET: #all Students


 Set the request type to GET.
 Enter the URL: http://localhost:3000/students.
 Click on the "Send" button
 You should receive a response with details of all students in your SQLite database.
DELETE:
 Set the request type to DELETE.
 Enter the URL for the student you want to delete (replace: id with an actual student ID):
http://localhost:3000/students/:id
 Place instead of ID which replace with number that is ID to be deleted.
 Then click send
PUT:
 Set the request type to PUT.
 Enter the URL for the student you want to delete (replace: id with an actual student ID):
http://localhost:3000/students/:id
 Go to the "Body" tab.
 Select raw and set the body to JSON format.

You might also like