> For the complete documentation index, see [llms.txt](https://guide-node-js.wishtack.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide-node-js.wishtack.io/express/routing.md).

# Routing

Le *routing* définit le **lien** entre les **URLs** d'une application et le **traitement** à effectuer pour chaque requête.

Express permet de définir le *routing* en établissant un lien entre une méthode HTTP, un *path* et la fonction qui sera exécutée à chaque requête.

```javascript
app.METHOD(path, callback);
```

### Exemples

```javascript
app.get('/', (req, res) => res.send('Welcome!'));

/* Activate body parser for both url-encoded and JSON data. */
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

app.post('/users', (req, res) => {
    console.log(req.body.firstName);
    res.end();
});
```

### Il est possible de configurer le *routing* avec des expressions régulières

```javascript
app.get(/^\/blogs\/(\d+)$/, (req, res) => {
    const blogId = req.params[0];
    ...
});
```

#### APIs des objets `request` et `response`

<http://expressjs.com/en/4x/api.html#req>

{% embed url="<http://expressjs.com/en/4x/api.html#req>" %}

\
<http://expressjs.com/en/4x/api.html#res>

{% embed url="<http://expressjs.com/en/4x/api.html#res>" %}

{% hint style="warning" %}
Express étant asynchrone, si aucune réponse n'est envoyée explicitement au client, la connexion sera maintenue jusqu'au timeout.
{% endhint %}

{% hint style="warning" %}
Certaines fonctions utilisent des callbacks distinctes pour gérer le cas de succès et le cas d'erreur. Si vous oubliez de gérer le cas d'erreur, on tombe dans le cas du "timeout".
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guide-node-js.wishtack.io/express/routing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
