slackapi / bolt-js

A framework to build Slack apps using JavaScript
https://tools.slack.dev/bolt-js/
MIT License
2.74k stars 393 forks source link

how to attach middleware like express.static ? #587

Closed dcsan closed 4 years ago

dcsan commented 4 years ago

Description

I want to send some static files from an app that's also using Bolt.

as per the docs here I can get the bolt "router" but can't use the normal method to attach express.static middleware.

    const receiver = new ExpressReceiver({ signingSecret: AppConfig.signingSecret });
    app.use(eventLogger) // my own custom middleware

    // but this doesnt work
    // app.use('/cdn', express.static('/cdn'))

currently I have to wire it up with manual send file:

    receiver.router.get('/cdn/*', (req, res) => {
      logger.log('cnd get', req.path)
      const fp = path.join(__dirname, '../../../', req.path)
      res.contentType('image/jpeg');
      res.sendFile(fp);
    })

What type of issue is this? (place an x in one of the [ ])

Requirements (place an x in each of the [ ])

seratch commented 4 years ago

// but this doesnt work // app.use('/cdn', express.static('/cdn'))

receiver.app.use('/cdn', express.static('/cdn')) should work for you. As App should not provide any Express.js specific methods or interfaces, we won't add the functionalities to App. You can directly access Express by either receiver.router or receiver.app.

See also: https://github.com/slackapi/bolt-js/issues/191

seratch commented 4 years ago

@dcsan I believe I've already answered this question. Could you close this issue?

dcsan commented 4 years ago

yep this worked, thanks!

jzaynal commented 1 year ago

@seratch using a custom ExpressReceiver as the receiver property into App is not always an option. As soon as you provide the receiver option the built-in Bolt listeners aren't usable. I am currently using the built-in listeners and endpoints for OAuth, events and actions, so want to keep using those but at the same time also configure a static files endpoint.

How can I do that without losing all the OAuth and event listeners that come with Bolt?

Bolt for JavaScript does not support OAuth for custom receivers. If you’re implementing a custom receiver, you can use our Slack OAuth library, which is what Bolt for JavaScript uses under the hood.

https://slack.dev/bolt-js/concepts#authenticating-oauth