From 49744a79e7d5e4964ea5f1d8903615fe5b134360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Apagyi=20D=C3=A1vid?= Date: Wed, 2 May 2018 21:44:47 +0200 Subject: [PATCH 1/2] Add getRouter() function --- lib/BootBot.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/BootBot.js b/lib/BootBot.js index cf0f979..2844577 100644 --- a/lib/BootBot.js +++ b/lib/BootBot.js @@ -50,6 +50,16 @@ class BootBot extends EventEmitter { }); } + /** + * Returns the express router, that BootBot uses. + * It can be used, when you can only use one port. + */ + getRouter(){ + this._initWebhook(); + console.log('BootBot running on the same port as express'); + return this.app; + } + /** * Closes the express server (calls `.close()` on the server instance). */ From 8ff4f10db77ca29d9f1e55fc3f91deda727aa280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Apagyi=20D=C3=A1vid?= Date: Wed, 2 May 2018 22:21:37 +0200 Subject: [PATCH 2/2] Add documentation, using express.Router() --- README.md | 12 ++++++++++++ lib/BootBot.js | 13 +++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b40e31b..8816dcd 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,18 @@ If you want to specify a custom endpoint name for your webhook, you can do it wi Starts the express server on the specified port. Defaults port to 3000. +#### `.getRouter()` +Returns the express router, that BootBot uses. It can be [used](https://expressjs.com/en/guide/routing.html#express-router), when you can only use one port. + + +##### `.getRouter()` examples: + +```javascript +app.use('/bootbot', bot.getRouter()); + +app.listen(3000, () => console.log('BootBot and Express are listening on port 3000!')); +``` + #### `.close()` Closes the express server (calls `.close()` on the server instance). diff --git a/lib/BootBot.js b/lib/BootBot.js index 2844577..a27d888 100644 --- a/lib/BootBot.js +++ b/lib/BootBot.js @@ -29,6 +29,7 @@ class BootBot extends EventEmitter { this.appSecret = options.appSecret; this.broadcastEchoes = options.broadcastEchoes || false; this.app = express(); + this.router = express.Router(); this.webhook = options.webhook || '/webhook'; this.webhook = this.webhook.charAt(0) !== '/' ? `/${this.webhook}` : this.webhook; this.app.use(bodyParser.json({ verify: this._verifyRequestSignature.bind(this) })); @@ -41,7 +42,7 @@ class BootBot extends EventEmitter { * @param {Number} [port=3000] */ start(port) { - this._initWebhook(); + this._initWebhook('app'); this.app.set('port', port || 3000); this.server = this.app.listen(this.app.get('port'), () => { const portNum = this.app.get('port'); @@ -55,9 +56,9 @@ class BootBot extends EventEmitter { * It can be used, when you can only use one port. */ getRouter(){ - this._initWebhook(); + this._initWebhook('router'); console.log('BootBot running on the same port as express'); - return this.app; + return this.router; } /** @@ -607,8 +608,8 @@ class BootBot extends EventEmitter { } - _initWebhook() { - this.app.get(this.webhook, (req, res) => { + _initWebhook(app='app') { + this[app].get(this.webhook, (req, res) => { if (req.query['hub.mode'] === 'subscribe' && req.query['hub.verify_token'] === this.verifyToken) { console.log('Validation Succeded.') res.status(200).send(req.query['hub.challenge']); @@ -618,7 +619,7 @@ class BootBot extends EventEmitter { } }); - this.app.post(this.webhook, (req, res) => { + this[app].post(this.webhook, (req, res) => { var data = req.body; if (data.object !== 'page') { return;