express get url parameters
const express = require('express');
const app = express();
app.get('/user/:id', (req, res) => {
res.send('User ID: ' + req.params.id);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
This Node.js code uses the Express framework to create a server. It defines a route using app.get()
for the '/user/:id' URL pattern. When a request is made to this route, the callback function (req, res) => { ... }
is executed. Inside this function, req.params.id
retrieves the value of the 'id' parameter from the URL. The server responds by sending a message containing the retrieved 'id' parameter to the client. The app.listen()
method starts the server on port 3000, and a message is logged to the console confirming the server is running.