Skip to content

Commit c003256

Browse files
committed
Changed route nylas-webhook to /taas/nylas-webhooks, only nylas webhook would use custom express middleware
1 parent 80c4784 commit c003256

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ ngrok http 3000
270270
271271
in https://dashboard.nylas.com/applications/{id} create nylas webhook url with created/updated/deleted event trigger
272272
```
273-
https://{generatedId}.ngrok.io/api/v5/nylas-webhook
273+
https://{generatedId}.ngrok.io/api/v5/taas/nylas-webhooks
274274
```
275275
276276

app.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,31 @@ app.use(cors({
2525
// Allow browsers access pagination data in headers
2626
exposedHeaders: ['X-Page', 'X-Per-Page', 'X-Total', 'X-Total-Pages', 'X-Prev-Page', 'X-Next-Page']
2727
}))
28-
// app.use(express.json())
29-
// For test nylas webhook, we need raw buffer
30-
// Here i sCustom Middleware to compute rawBody. Unfortunately using
31-
// JSON.stringify(req.body) will remove spaces and newlines, so verification
32-
// will fail. We must add this middleware to ensure we're computing the correct
33-
// signature
34-
app.use(function (req, res, next) {
35-
req.rawBody = ''
36-
req.on('data', (chunk) => (req.rawBody += chunk))
37-
req.on('error', () => res.status(500).send('Error parsing body'))
38-
39-
req.on('end', () => {
40-
// because the stream has been consumed, other parsers like bodyParser.json
41-
// cannot stream the request data and will time out so we must explicitly parse the body
42-
try {
43-
req.body = req.rawBody.length ? JSON.parse(req.rawBody) : {}
44-
next()
45-
} catch (err) {
46-
res.status(500).send('Error parsing body')
47-
}
48-
})
28+
app.use((...args) => {
29+
const [req, res, next] = args;
30+
// For test nylas webhook, we need raw buffer
31+
// Here i sCustom Middleware to compute rawBody. Unfortunately using
32+
// JSON.stringify(req.body) will remove spaces and newlines, so verification
33+
// will fail. We must add this middleware to ensure we're computing the correct
34+
// signature
35+
if(req.path === `${config.BASE_PATH}/taas/nylas-webhooks`) {
36+
req.rawBody = ''
37+
req.on('data', (chunk) => (req.rawBody += chunk))
38+
req.on('error', () => res.status(500).send('Error parsing body'))
39+
40+
req.on('end', () => {
41+
// because the stream has been consumed, other parsers like bodyParser.json
42+
// cannot stream the request data and will time out so we must explicitly parse the body
43+
try {
44+
req.body = req.rawBody.length ? JSON.parse(req.rawBody) : {}
45+
next()
46+
} catch (err) {
47+
res.status(500).send('Error parsing body')
48+
}
49+
})
50+
return
51+
}
52+
return express.json()(...args)
4953
})
5054
app.use(express.urlencoded({ extended: true }))
5155
app.set('port', config.PORT)

src/routes/WebhookRoutes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
module.exports = {
6-
'/nylas-webhook': {
6+
'/taas/nylas-webhooks': {
77
post: {
88
controller: 'WebhookController',
99
method: 'nylasWebhook'

0 commit comments

Comments
 (0)