ExpressのRouting
例
tsconst app = express();
app.get('/', (req, res) => {});
app.post('/books', (req, res) => {});
app.put('/books/:id', (req, res) => {});
app.delete('/books/:id', (req, res) => {});
pathの部分には正規表現も使える
使うことあるのか?という気もする

めっちゃく複雑になりそう
tsrouter.get(
'/:id',
(req, res, next) => {
const value = { value: req.params.id };
res.send(value);
next();
},
(_, __, next) => {
console.log('hogehoge');
// next(); // ここをコメントアウトすると、
},
() => {
console.log('fugafuga'); // ここは呼ばれない
}
);
上から順にhandlerが呼ばれていく
ただし、 next()
を呼ばないと次のhandlerは呼ばれない
あるとすれば、例えば、
1個目のhandlerではvalidationして、
2個目で実際の処理をして、
とかやっていくとか?
でもこれも1つのhandler内に両方書いてもさほど問題なくない?という気がする

next()
実行し忘れとかダルいし
上のように書くと、単純に見づらい

配列でも渡せる
tsconst 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])
うーん、なるほど?
tsapp.route('/book')
.get(function (req, res) {
res.send('Get a random book')
})
.post(function (req, res) {
res.send('Add a book')
})
.put(function (req, res) {
res.send('Update the book')
})
ここの凝集性がどこまで必要かは、考える余地がありそう

「探しやすいのか?」がどんなものか気になるところ