Handling GET requests in an Express.js application involves defining routes that respond to GET HTTP requests. Here's a basic example of handling a GET request in an Express.js application:
const express = require('express');
const app = express();
const port = 3000;
// Define a simple GET route
app.get('/', (req, res) => {
res.send('Hello, this is a GET request!');
});
// Define a route with a parameter
app.get('/greet/:name', (req, res) => {
const { name } = req.params;
res.send(`Hello, ${name}!`);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on <http://localhost>:${port}`);
});
In this example:
express()
.'/'
) that responds with a message.'/greet/:name'
) that responds with a personalized greeting based on the parameter.app.listen
on port 3000.When you run this script (node filename.js
) and visit http://localhost:3000
in your browser, you should see the response from the root route. Additionally, visiting http://localhost:3000/greet/John
should display a personalized greeting for the name "John."
This is a basic example, and in a real-world application, you would likely have more complex routes and logic. Express provides a flexible and powerful routing system, allowing you to handle different HTTP methods, route parameters, query parameters, and more.