Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include an example of using middleware with the ExpressReceiver #1973

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch 👍

console.log('⚡️ Bolt app started');
})();
```
</details>