@@ -11,6 +11,7 @@ const bodyParser = require('body-parser');
1111const OpenApiValidator = require ( 'express-openapi-validator' ) ;
1212const logger = require ( './logger' ) ;
1313const config = require ( './config' ) ;
14+ const interceptor = require ( 'express-interceptor' ) ;
1415
1516class ExpressServer {
1617 constructor ( port , openApiYaml ) {
@@ -46,12 +47,37 @@ class ExpressServer {
4647 res . status ( 200 ) ;
4748 res . json ( req . query ) ;
4849 } ) ;
50+ // Send back info from backend to client using long polling
51+ // 1. Create long polling endpoint
52+ const longPoll = require ( "express-longpoll" ) ( this . app )
53+ longPoll . create ( "/poll" ) ;
54+
55+ // 2. Intercept all responses (to server-server calls) and send them to the client through long polling endpoint
56+ const finalLongPollingInterceptor = interceptor ( function ( req , res ) {
57+ return {
58+ isInterceptable : ( ) => true ,
59+ // Sends response to the client through long polling endpoint
60+ intercept : function ( body , send ) {
61+ const data = {
62+ code : res . statusCode ,
63+ body
64+ } ;
65+ console . log ( data ) ;
66+ longPoll . publish ( "/poll" , data ) ;
67+ send ( body ) ;
68+ }
69+ } ;
70+ } )
71+ // Add the interceptor middleware
72+ this . app . use ( finalLongPollingInterceptor ) ;
4973 }
5074
5175 launch ( ) {
5276 this . app . use (
5377 OpenApiValidator . middleware ( {
5478 apiSpec : this . openApiPath ,
79+ // Automatic mapping of OpenAPI endpoints to Express handler functions
80+ // Ref.: https://github.com/cdimascio/express-openapi-validator/wiki/Documentation#example-express-api-server-with-operationhandlers
5581 operationHandlers : path . join ( __dirname ) ,
5682 fileUploader : { dest : config . FILE_UPLOAD_PATH } ,
5783 } )
0 commit comments