Nella seguente funzione Express:
app.get('/user/:id', function(req, res){
res.send('user' + req.params.id);
});
Cosa sono reqe res? Cosa rappresentano, cosa significano e cosa fanno?
Grazie!
Nella seguente funzione Express:
app.get('/user/:id', function(req, res){
res.send('user' + req.params.id);
});
Cosa sono reqe res? Cosa rappresentano, cosa significano e cosa fanno?
Grazie!
Risposte:
reqè un oggetto contenente informazioni sulla richiesta HTTP che ha generato l'evento. In risposta a req, si utilizza resper inviare indietro la risposta HTTP desiderata.
Quei parametri possono essere nominati qualunque cosa. È possibile modificare questo codice in questo se è più chiaro:
app.get('/user/:id', function(request, response){
response.send('user ' + request.params.id);
});
Modificare:
Supponi di avere questo metodo:
app.get('/people.json', function(request, response) { });
La richiesta sarà un oggetto con proprietà come queste (solo per citarne alcuni):
request.url, che sarà "/people.json"quando viene attivata questa particolare azionerequest.method, che sarà "GET"in questo caso, da qui la app.get()chiamata.request.headers, contenente elementi come request.headers.accept, che è possibile utilizzare per determinare quale tipo di browser ha effettuato la richiesta, che tipo di risposte è in grado di gestire, se è in grado di comprendere la compressione HTTP, ecc.request.query(ad es. /people.json?foo=barPorterebbe a request.query.foocontenere la stringa "bar").Per rispondere a tale richiesta, utilizzare l'oggetto response per creare la propria risposta. Per espandere l' people.jsonesempio:
app.get('/people.json', function(request, response) {
// We want to set the content-type header so that the browser understands
// the content of the response.
response.contentType('application/json');
// Normally, the data is fetched from a database, but we can cheat:
var people = [
{ name: 'Dave', location: 'Atlanta' },
{ name: 'Santa Claus', location: 'North Pole' },
{ name: 'Man in the Moon', location: 'The Moon' }
];
// Since the request is for a JSON representation of the people, we
// should JSON serialize them. The built-in JSON.stringify() function
// does that.
var peopleJSON = JSON.stringify(people);
// Now, we can use the response object's send method to push that string
// of people JSON back to the browser in response to this request:
response.send(peopleJSON);
});
reqe resla struttura, è descritto nei documenti espressi: req: expressjs.com/en/api.html#req , res: expressjs.com/en/api.html#res
Ho notato un errore nella risposta di Dave Ward (forse una recente modifica?): I parametri della stringa di query sono presenti request.query, no request.params. (Vedi https://stackoverflow.com/a/6913287/166530 )
request.params per impostazione predefinita viene riempito con il valore di qualsiasi "corrispondenza componente" nelle route, ad es
app.get('/user/:id', function(request, response){
response.send('user ' + request.params.id);
});
e, se hai configurato express per usare il suo bodyparser ( app.use(express.bodyParser());) anche con i formdata POST. (Vedi Come recuperare i parametri della query POST? )
req=="request"//res=="response"