AIDS 7 Express JS
7.1 Implement routing for the
a)
application by embedding the necessary code in the routes/route.js file.
// Import required modules
const express = require('express');
const router = express.Router();
// Define routes
router.get('/', function(req, res) {
res.send('Hello, world!');
});
router.get('/about', function(req, res) {
res.send('This is the about page');
});
router.get('/contact', function(req, res) {
res.send('This is the contact page');
});
// Export the router so it can be used in other files
module.exports = router;
save it in ./routes/router.js
// Import required modules
const express = require('express');
const app = express();
// Import the router
const routes = require('./routes/router.js');
// Mount the router as middleware
app.use('/', routes);
// Start the server
const PORT = 3000;
app.listen(PORT, function() {
console.log(`Server listening on port ${PORT}`);
});
save it and run... open browser and type
1. localhost:3000
2. localhost:3000/about
3. localhost:3000/contact to see the result
7a)
Defining a route, Handling Routes, Route Parameters, Query Parameters
Implement routing for the AdventureTrails application by embedding the necessary code in
the routes/route.js file.
EXPRESS.JS
create a directory MyProject2
cd MyProject2
npm init -y
to create package.json file
npm install express
to install express
Program:
In project folder create a folder with name routes, in side routes folder create the following
file and name it as route.js
const express = require('express');
const router = express.Router();
// Sample data for AdventureTrails
const trails = [
{ id: 1, name: "Mountain Trek", difficulty: "Hard" },
{ id: 2, name: "Forest Walk", difficulty: "Easy" },
{ id: 3, name: "Desert Expedition", difficulty: "Medium" },
{ id:4,name:"SkyDiving",difficulty:"Hard"},
{id:5,name:"pool Jumping",difficulty:"Medium"},
{id:6,name:"Para Gliding",difficulty:"Hard"},
{id:7,name:"Go Karting",difficulty:"Medium"}
];
/**
* 1. Defining a Route (Home Route)
*/
router.get('/', (req, res) => {
res.send('Welcome to AdventureTrails!');
});
/**
* 2. Handling Routes - Fetch all trails
*/
router.get('/trails', (req, res) => {
res.json(trails);
});
/**
* 3. Route Parameters - Fetch a specific trail by ID
* Example: GET /trails/2
*/
router.get('/trails/:id', (req, res) => {
const trailId = parseInt(req.params.id);
const trail = trails.find(t => t.id === trailId);
if (!trail) {
return res.status(404).json({ message: "Trail not found" });
}
res.json(trail);
});
/**
* 4. Query Parameters - Filter trails by difficulty
* Example: GET /trails/filter?difficulty=Easy
*/
router.get('/trails/filter', (req, res) => {
const difficulty = req.query.difficulty; // Extract the 'difficulty' query parameter
if (!difficulty) {
return res.status(400).json({ message: "Please provide a difficulty level" });
}
const filteredTrails = trails.filter(t => t.difficulty.toLowerCase() === difficulty.toLowerCase());
if (filteredTrails.length === 0) {
return res.status(404).json({ message: "No trails found for the specified difficulty" });
}
res.json(filteredTrails);
});
module.exports = router;
**************create a file called server.js in project folder and
run
open http://localhost:3000/api to test
Test cases:
|
API Endpoint |
Purpose |
Example URL |
||
|
Home Route |
Check if the API is working |
http://localhost:3000/api |
||
|
Fetch all trails |
|
http://localhost:3000/api/trails |
||
|
Fetch a specific trail by ID |
Get details of a trail by ID |
http://localhost:3000/api/trails/2 |
||
|
Filter trails by difficulty |
Get trails of a specific difficulty |
http://localhost:3000/api/trails/filter?difficulty=Hard |
Comments
Post a Comment