🎓 A1Learner
🔍
0 of 50 lessons completed

Route Parameters

A route parameter is a named blank inside an address. One route with a blank can answer many addresses that share the same shape.

A route with a blank

This route answers any address shaped like /users/asha.

app.get('/users/:name', (req, res) => {
  res.send('Hello, ' + req.params.name + '!');
});

Read the value

This line reads the value that filled the blank.

console.log(req.params.name);

Example

const express = require('express');

const app = express();

app.get('/users/:name', (req, res) => {
  res.send('Hello, ' + req.params.name + '!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Key points

  • A route parameter is a named blank inside an address.
  • A colon marks a parameter, as in /users/:name.
  • The name after the colon is the name of the parameter.
  • One route with a parameter answers many addresses.
  • req.params holds the values from the address.
  • req.params.name holds the value for the parameter called name.

In this example, the route holds one blank called name. A visit to /users/asha fills the blank with asha. The server answers Hello, asha!. The startup line prints Server running on port 3000.

Note

The value from the address is always a string of text.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A visit opens /users/asha. What does req.params.name hold?