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.