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

Understanding req.params in Express

The document contains an Express.js API for managing student records, allowing for the creation, retrieval, updating, and deletion of students. It includes routes for handling requests related to students and uses a simple in-memory data structure for storage. The server listens on port 3000 and utilizes body-parser middleware to handle JSON requests.

Uploaded by

jaganmohan.csd
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)
59 views3 pages

Understanding req.params in Express

The document contains an Express.js API for managing student records, allowing for the creation, retrieval, updating, and deletion of students. It includes routes for handling requests related to students and uses a simple in-memory data structure for storage. The server listens on port 3000 and utilizes body-parser middleware to handle JSON requests.

Uploaded by

jaganmohan.csd
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

Api.

js

const express = require('express');

const router = [Link]();

const students = require('../data');

// Create a new student

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

const newStudent = [Link];

[Link] = [Link] + 1;

[Link](newStudent);

[Link](newStudent);

});

// Get all students

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

[Link](students);

});

// Get a single student

[Link]('/students/:id', (req, res) => {

const id = parseInt([Link]);

const student = [Link](s => [Link] === id);

if (!student) {

[Link](404).send({ message: 'Student not found' });

} else {

[Link](student);

});

// Update a student

[Link]('/students/:id', (req, res) => {

const id = parseInt([Link]);

const student = [Link](s => [Link] === id);


if (!student) {

[Link](404).send({ message: 'Student not found' });

} else {

[Link](student, [Link]);

[Link](student);

});

// Delete a student

[Link]('/students/:id', (req, res) => {

const id = parseInt([Link]);

const index = [Link](s => [Link] === id);

if (index === -1) {

[Link](404).send({ message: 'Student not found' });

} else {

[Link](index, 1);

[Link]({ message: 'Student deleted successfully' });

});

[Link] = router;

[Link]

const express = require('express');

const app = express();

const bodyParser = require('body-parser');

const apiRoutes = require('./routes');

[Link]([Link]());

[Link]('/routes', apiRoutes);

[Link](3000, () => {
[Link]('Server started on port 3000');

});

[Link]

const students = [

{ id: 1, name: 'John Doe', email: 'john@[Link]', rollNumber: 12345 },

{ id: 2, name: 'Jane Doe', email: 'jane@[Link]', rollNumber: 67890 },

];

[Link] = students;

You might also like