Skip to content

Commit

Permalink
Include an example of using middleware with the ExpressReceiver (#1973
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zimeg committed Oct 18, 2023
1 parent f104c0b commit e2ac3ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 7 additions & 1 deletion docs/_advanced/custom_routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const app = new App({
</summary>

<div class="secondary-content" markdown="0">
Adding custom HTTP routes is quite straightforward when using Bolt’s built-in ExpressReceiver. Since `v2.1.0`, `ExpressReceiver` added a `router` property, which exposes the Express [Router](http://expressjs.com/en/4x/api.html#router) on which additional routes can be added.
Adding custom HTTP routes is quite straightforward when using Bolt’s built-in ExpressReceiver. Since `v2.1.0`, `ExpressReceiver` added a `router` property, which exposes the Express [Router](http://expressjs.com/en/4x/api.html#router) on which additional routes and middleware can be added.
</div>

```javascript
Expand All @@ -78,6 +78,12 @@ app.event('message', async ({ event, client }) => {
await client.chat.postMessage(...);
});

// Middleware methods execute on every web request
receiver.router.use((req, res, next) => {
console.log(`Request time: ${Date.now()}`);
next();
});

// Other web requests are methods on receiver.router
receiver.router.post('/secret-page', (req, res) => {
// You're working with an express req and res now.
Expand Down
7 changes: 6 additions & 1 deletion docs/_advanced/ja_custom_routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ app.event('message', async ({ event, client }) => {
await client.chat.postMessage(...);
});

receiver.router.use((req, res, next) => {
console.log(`Request time: ${Date.now()}`);
next();
});

// それ以外の Web リクエストの処理は receiver.router のメソッドで定義
receiver.router.post('/secret-page', (req, res) => {
// ここでは Express のリクエストやレスポンスをそのまま扱う
Expand All @@ -86,7 +91,7 @@ receiver.router.post('/secret-page', (req, res) => {

(async () => {
await app.start();
console.log('⚡️ Bolt app started'');
console.log('⚡️ Bolt app started');
})();
```
</details>

0 comments on commit e2ac3ac

Please sign in to comment.