In Express.js, handling HTTP GET requests is a fundamental aspect of building web applications.
Express provides a straightforward way to define routes for handling these requests.
const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; // Handling a simple GET request at the root URL "/" app.get('/', (req, res) => { res.send('Hello, World!'); }); // Listening on the specified port app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; // Handling a GET request with a specific route "/about" app.get('/about', (req, res) => { res.send('This is the About page.'); }); // Listening on the specified port app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; // Handling a GET request with parameters in the URL app.get('/user/:id', (req, res) => { const userId = req.params.id; res.send(`User ID: ${userId}`); }); // Listening on the specified port app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; // Handling a GET request with query parameters app.get('/search', (req, res) => { const query = req.query.name; res.send(`Search Query: ${query}`); }); // Listening on the specified port app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; // Handling a simple GET request at the root URL "/" app.get('/', (req, res) => { res.send('Hello, World!'); }); // Handling a GET request with a specific route "/about" app.get('/about', (req, res) => { res.send('This is the About page.'); }); // Handling a GET request with parameters in the URL app.get('/user/:id', (req, res) => { const userId = req.params.id; res.send(`User ID: ${userId}`); }); // Handling a GET request with query parameters app.get('/search', (req, res) => { const query = req.query.name; res.send(`Search Query: ${query}`); }); // Listening on the specified port app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
"app.get()" is used to define a route for handling HTTP GET requests.
The first parameter of app.get() is the path or route, and the second parameter is a callback function that gets executed when a request is made to that route.
The req (request) object provides information about the incoming request, and the res (response) object is used to send a response back to the client.
Handling a simple GET request at the root URL ("/").
Handling a GET request with a specific route ("/about").
Handling a GET request with parameters in the URL ("/user/:id").
Handling a GET request with query parameters ("/search?name=Alice").
// navigate to folder location where server.js exist node server.js