@@ -25,27 +25,31 @@ app.use(cors({
25
25
// Allow browsers access pagination data in headers
26
26
exposedHeaders : [ 'X-Page' , 'X-Per-Page' , 'X-Total' , 'X-Total-Pages' , 'X-Prev-Page' , 'X-Next-Page' ]
27
27
} ) )
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 )
49
53
} )
50
54
app . use ( express . urlencoded ( { extended : true } ) )
51
55
app . set ( 'port' , config . PORT )
0 commit comments