Routing
Routing fa riferimento alla definizione di endpoint dell’applicazione (URI) e alla loro modalità di risposta alle richieste del client. Per un’introduzione al concetto di routing, consultare la sezione Routing di base.
You define routing using methods of the Express app
object that correspond to HTTP methods;
for example, app.get()
to handle GET requests and app.post
to handle POST requests. For a full list,
see app.METHOD. You can also use app.all() to handle all HTTP methods and app.use() to
specify middleware as the callback function (See Using middleware for details).
These routing methods specify a callback function (sometimes called “handler functions”) called when the application receives a request to the specified route (endpoint) and HTTP method. In other words, the application “listens” for requests that match the specified route(s) and method(s), and when it detects a match, it calls the specified callback function.
In fact, the routing methods can have more than one callback function as arguments.
With multiple callback functions, it is important to provide next
as an argument to the callback function and then call next()
within the body of the function to hand off control
to the next callback.
Il codice seguente è un esempio di una route veramente di base.
const express = require('express')
const app = express()
// respond with "hello world" when a GET request is made to the homepage
app.get('/', (req, res) => {
res.send('hello world')
})
Metodi di route
Un metodo di route deriva da uno dei metodi HTTP ed è collegato ad un’istanza delle classe express
.
Il codice seguente è un esempio di route definite per i metodi GET e POST nella root dell’app.
// GET method route
app.get('/', (req, res) => {
res.send('GET request to the homepage')
})
// POST method route
app.post('/', (req, res) => {
res.send('POST request to the homepage')
})
Express supports methods that correspond to all HTTP request methods: get
, post
, and so on.
For a full list, see app.METHOD.
Esiste un metodo di routing speciale, app.all()
, che non deriva da alcun metodo HTTP. Questo metodo viene utilizzato per caricare funzioni middleware in un percorso per tutti i metodi di richiesta.
app.all('/secret', (req, res, next) => {
console.log('Accessing the secret section ...')
next() // pass control to the next handler
})
Percorsi di route
I percorsi di route, in combinazione con un metodo di richiesta, definiscono gli endpoint a cui possono essere rivolte richieste. I percorsi di route possono essere stringhe, modelli di stringa o espressioni regolari.
Caution
In express 5, the characters ?
, +
, *
, []
, and ()
are handled differently than in version 4, please review the migration guide for more information.
Caution
In express 4, regular expression characters such as $
need to be escaped with a \
.
Note
Express uses path-to-regexp for matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths. Express Route Tester è uno strumento utile per il test delle route Express di base, anche se non supporta la corrispondenza di modelli.
Warning
Query strings are not part of the route path.
Ecco alcuni esempi di percorsi di route basati su stringhe.
Questo percorso di route corrisponderà a richieste nella route root, /
.
app.get('/', (req, res) => {
res.send('root')
})
Questo percorso di route corrisponderà a richieste in /about
.
app.get('/about', (req, res) => {
res.send('about')
})
Questo percorso di route corrisponderà a richieste in /random.text
.
app.get('/random.text', (req, res) => {
res.send('random.text')
})
Ecco alcuni esempi di percorsi di route basati su modelli di stringa.
Caution
The string patterns in Express 5 no longer work. Please refer to the migration guide for more information.
Questo percorso di route corrisponderà a acd
e abcd
.
app.get('/ab?cd', (req, res) => {
res.send('ab?cd')
})
Questo percorso di route corrisponderà a abcd
, abbcd
, abbbcd
e così via.
app.get('/ab+cd', (req, res) => {
res.send('ab+cd')
})
Questo percorso di route corrisponderà a abcd
, abxcd
, abRABDOMcd
, ab123cd
e così via.
app.get('/ab*cd', (req, res) => {
res.send('ab*cd')
})
Questo percorso di route corrisponderà a /abe
e /abcde
.
app.get('/ab(cd)?e', (req, res) => {
res.send('ab(cd)?e')
})
Esempi di percorsi di route basati su espressioni regolari:
Questo percorso di route corrisponderà a qualsiasi elemento con “a” nel nome route.
app.get(/a/, (req, res) => {
res.send('/a/')
})
Questo percorso di route corrisponderà a butterfly
e dragonfly
, ma non a butterflyman
, dragonfly man
e così via.
app.get(/.*fly$/, (req, res) => {
res.send('/.*fly$/')
})
Le stringhe di query non fanno parte del percorso di route.
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params
object, with the name of the route parameter specified in the path as their respective keys.
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }
To define routes with route parameters, simply specify the route parameters in the path of the route as shown below.
app.get('/users/:userId/books/:bookId', (req, res) => {
res.send(req.params)
})
The name of route parameters must be made up of “word characters” ([A-Za-z0-9_]).
Since the hyphen (-
) and the dot (.
) are interpreted literally, they can be used along with route parameters for useful purposes.
Route path: /flights/:from-:to
Request URL: http://localhost:3000/flights/LAX-SFO
req.params: { "from": "LAX", "to": "SFO" }
Route path: /plantae/:genus.:species
Request URL: http://localhost:3000/plantae/Prunus.persica
req.params: { "genus": "Prunus", "species": "persica" }
Caution
In express 5, Regexp characters are not supported in route paths, for more information please refer to the migration guide.
To have more control over the exact string that can be matched by a route parameter, you can append a regular expression in parentheses (()
):
Route path: /user/:userId(\d+)
Request URL: http://localhost:3000/user/42
req.params: {"userId": "42"}
Warning
Because the regular expression is usually part of a literal string, be sure to escape any \
characters with an additional backslash, for example \\d+
.
Warning
In Express 4.x, the *
character in regular expressions is not interpreted in the usual way. As a workaround, use {0,}
instead of *
. This will likely be fixed in Express 5.
Handler di route
È possibile fornire molteplici funzioni di callback che si comportino come middleware per gestire una richiesta. La sola eccezione è rappresentata dal fatto che queste callback potrebbero richiamare next('route')
per ignorare le callback di route restanti. È possibile utilizzare questo meccanismo per imporre pre-condizioni su una route, quindi, passare il controllo a route successive, nel caso non ci siano motivi per proseguire con la route corrente.
Gli handler di route possono avere il formato di una funzione, di un array di funzioni o di combinazioni di entrambi, come illustrato nei seguenti esempi.
Una singola funzione di callback può gestire una route. Ad esempio:
app.get('/example/a', (req, res) => {
res.send('Hello from A!')
})
Più funzioni di callback possono gestire una route (assicurarsi di specificare l’oggetto next
). Ad esempio:
app.get('/example/b', (req, res, next) => {
console.log('the response will be sent by the next function ...')
next()
}, (req, res) => {
res.send('Hello from B!')
})
Un array di funzioni callback possono gestire una route. Ad esempio:
const cb0 = function (req, res, next) {
console.log('CB0')
next()
}
const cb1 = function (req, res, next) {
console.log('CB1')
next()
}
const cb2 = function (req, res) {
res.send('Hello from C!')
}
app.get('/example/c', [cb0, cb1, cb2])
Una combinazione di funzioni indipendenti e array di funzioni può gestire una route. Ad esempio:
const cb0 = function (req, res, next) {
console.log('CB0')
next()
}
const cb1 = function (req, res, next) {
console.log('CB1')
next()
}
app.get('/example/d', [cb0, cb1], (req, res, next) => {
console.log('the response will be sent by the next function ...')
next()
}, (req, res) => {
res.send('Hello from D!')
})
Metodi di risposta
I metodi sull’oggetto risposta (res
) nella seguente tabella possono inviare una risposta al client e terminare il ciclo richiesta-risposta. Se nessuno di questi metodi viene richiamato da un handler di route, la richiesta del client verrà lasciata in sospeso.
Metodo | Descrizione |
---|---|
res.download() | Richiedere un file da scaricare. |
res.end() | Terminare il processo di risposta. |
res.json() | Inviare una risposta JSON. |
res.jsonp() | Inviare una risposta JSON con supporto JSONP. |
res.redirect() | Reindirizzare una richiesta. |
res.render() | Eseguire il rendering di un template di vista. |
res.send() | Inviare una risposta di vari tipi. |
res.sendFile | Inviare un file come un flusso di ottetti. |
res.sendStatus() | Impostare il codice di stato della risposta e inviare la relativa rappresentazione di stringa come corpo della risposta. |
app.route()
È possibile creare handler di route concatenabili per un percorso di route, utilizzando app.route()
.
Poiché il percorso è specificato in una singola ubicazione, la creazione di route modulari è utile, come lo è la riduzione della ridondanza e degli errori tipografici. Per ulteriori informazioni sulle route, consultare: Documentazione su Router().
Ecco un esempio di handler di route concatenati, definiti utilizzando app.route()
.
app.route('/book')
.get((req, res) => {
res.send('Get a random book')
})
.post((req, res) => {
res.send('Add a book')
})
.put((req, res) => {
res.send('Update the book')
})
express.Router
Utilizzare la classe express.Router
per creare handler di route modulari, montabili. Un’istanza Router
è un middleware e un sistema di routing completo; per questa ragione, spesso si definisce “mini-app”.
Nel seguente esempio si crea un router come modulo, si carica al suo interno una funzione middleware, si definiscono alcune route e si monta un modulo router su un percorso nell’app principale.
Creare un file router denominato birds.js
nella directory app, con il seguente contenuto:
const express = require('express')
const router = express.Router()
// middleware that is specific to this router
const timeLog = (req, res, next) => {
console.log('Time: ', Date.now())
next()
}
router.use(timeLog)
// define the home page route
router.get('/', (req, res) => {
res.send('Birds home page')
})
// define the about route
router.get('/about', (req, res) => {
res.send('About birds')
})
module.exports = router
Successivamente, caricare il modulo router nell’applicazione:
const birds = require('./birds')
// ...
app.use('/birds', birds)
L’app ora sarà in grado di gestire richieste a /birds
e /birds/about
, oltre a richiamare la funzione middleware timeLog
, specifica per la route.
But if the parent route /birds
has path parameters, it will not be accessible by default from the sub-routes. To make it accessible, you will need to pass the mergeParams
option to the Router constructor reference.
const router = express.Router({ mergeParams: true })