回答:
reqイベントを発生させたHTTPリクエストに関する情報を含むオブジェクトです。への応答としてreq、res目的のHTTP応答を返信するために使用します。
これらのパラメーターには任意の名前を付けることができます。より明確であれば、そのコードを次のように変更できます。
app.get('/user/:id', function(request, response){
response.send('user ' + request.params.id);
});
編集:
あなたがこの方法を持っているとしましょう:
app.get('/people.json', function(request, response) { });
リクエストは次のようなプロパティを持つオブジェクトになります(ほんの数例を挙げると)。
request.url、"/people.json"この特定のアクションがトリガーされたときrequest.method、これは"GET"この場合であるため、app.get()呼び出しです。request.headersような項目が含まれてrequest.headers.acceptいます。これを使用して、要求を行ったブラウザの種類、処理できる応答の種類、HTTP圧縮を理解できるかどうかなどを決定できます。request.query(たとえば、文字列を含む/people.json?foo=bar結果にrequest.query.fooなります"bar")。その要求に応答するには、応答オブジェクトを使用して応答を作成します。people.json例を拡張するには:
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);
});
reqとres構造を探している場合、それはエクスプレスドキュメントで説明されていreqます::expressjs.com/en/api.html#req、res:expressjs.com/en/api.html#res
Dave Wardの回答に1つのエラーがあったことに気づきました(おそらく最近の変更ですか?):クエリ文字列パラメーターは、request.queryではなくにありrequest.paramsます。(https://stackoverflow.com/a/6913287/166530を参照)
request.params デフォルトでは、ルート内の「コンポーネントの一致」の値が入力されます。
app.get('/user/:id', function(request, response){
response.send('user ' + request.params.id);
});
app.use(express.bodyParser());また、POSTされたformdataでもbodyparser()を使用するようにexpressを設定している場合。(POSTクエリパラメータを取得する方法を参照してください。)
req=="request"//res=="response"