LoginSignup
152
112

More than 3 years have passed since last update.

Node.js Express  で出てくる req, res, next

Last updated at Posted at 2020-04-05

はじめに

Node.jsのExpressの雛形を生成するコマンド

express project-name

で出力されるコードの一部ですが

index.js
var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express', user: req.user });
});

このreq, res, nextは何なのか書いていこうと思います。

reqについて

reqapp.getの第一引数で指定されたパスに入ってきたHTTPリクエストを表すオブジェクトです。

var express = require('express');
var app = express();
app.get('/', func);
function func(req, res) {
    console.log(req.ip);
    console.log(req.method);
    console.log(req.path);
    console.log(req.protocol);
    console.log(req.query.name)

    res.end();
}

サーバを起動し(Portは8000)、

curl -X GET localhost:8000/hoge\?name=user1
::ffff:127.0.0.1
GET
/hoge
http
user1

とリクエストを送って出力を見てみましょう。ちなみにapp.getはGETメソッドでリクエストを受け付けます。最後のres.end()はHTTPレスポンスのプロセスを終了する関数です。これがないと処理が正常に完了しません。
このように来たHTTPリクエストに関する様々な情報を取得することができます。reqのプロパティやメソッドは他にもまだまだありますので詳しくは、
https://expressjs.com/ja/4x/api.html#req
を見てください。

resについて

resは指定されたパスに入ってきたリクエストに対するHTTPレスポンスを構成するためのオブジェクトです。

var express = require('express');
var app = express();
app.get('/', func);
function func(req, res) {
 if(req.query.name){
        res.status(200).send('Hello! your name : ' + req.query.name);
    }else{
        res.status(400).send('Who are you?');
    }
}

サーバを立てて、

curl -X GET localhost:8000/hoge\?name=user1
> Hello! your name : user1
curl -X GET localhost:8000/hoge
> Who are you?

とするとHTTPレスポンスが帰ってきます。status()でステータスコードを設定でき、send()でレスポンスボディを設定できます。
send()にはHTTPレスポンスプロセスの終了処理が含まれるので、end()は不要です。
resのプロパティやメソッドは、https://expressjs.com/ja/4x/api.html#res
を参考にしてください。

nextについて

通常は上記の、app.get('/hoge', func);のように、あるpath(/hoge)に対する、手続き(func)を呼んで、その中で、HTTPレスポンスを返して(res.end())終了します。

次のコードを見てください。


app.get('/hoge', preProcess1, preProcess2, mainProcess);
function preProcess1(req, res, next) {
    console.log('1個目の前処理したよ!');
}
function preProcess2(req, res, next) {
    console.log('2個目の前処理したよ!');
}
function mainProcess(req, res) {
    res.send('メインの処理をしたよ!');
}

実はこのように/hogeに対するアクションを何個も登録することができます。
まずはこれにリクエストを送ってみましょう。

curl -X GET localhost:8000/hoge

## server 側
1個目の処理をしたよ!

このようにpreProcess1の1個のみが実行されて、他は実行されませんでした。
preProcess1->preProcess2->mainProcessと処理を継続させたい時に使用するのがnextです。

app.get('/hoge', preProcess1, preProcess2, mainProcess);
function preProcess1(req, res, next) {
    console.log('1個目の前処理したよ!');
    next();
}
function preProcess2(req, res, next) {
    console.log('2個目の前処理したよ!');
    next();
}
function mainProcess(req, res) {
    res.send('メインの処理をしたよ!');
}

この場合、

curl -X GET localhost:8000/hoge
メインの処理をしたよ!
## server 側
1個目の処理をしたよ!
2個目の処理をしたよ!

と出力され、無事にHTTPレスポンスを返して処理を終えることができました。
next()関数によって、次の処理へ制御を渡すことができます。
https://expressjs.com/ja/guide/writing-middleware.html
ここによると、この場合の関数preProcess1やpreProcess2はmainProcessを行うための中間的な役割を行うものとして「ミドルウェア」と命名されています。
もしかしたらmainProcessも定義的にはミドルウェアかもしれないけど個人的にちょっと違和感があります。。。

参考文献

https://expressjs.com/ja/4x/api.html#req
https://expressjs.com/ja/4x/api.html#res
https://expressjs.com/ja/guide/writing-middleware.html

152
112
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
152
112