moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
291 stars 119 forks source link

How to access response object in a service called from API Gateway #282

Closed patel-jeel92 closed 2 years ago

patel-jeel92 commented 2 years ago

I have a Express js service that uses pugjs to render some files and send HTML as response. The way to do this is by using res.render(...) method in the service. I call this service from my API Gateway. In my Service A action, how can i access the response object so I can send html file back to the caller?

icebob commented 2 years ago

You can't, response is not serializable. You should generate the HTML content on the API gateway side in onAfterCall hook.

patel-jeel92 commented 2 years ago

Is there a way to only call onAfterCall for a specific endpoint. I dont want the html generated on each call.

icebob commented 2 years ago

you have access to ctx, so you can check when you need. E.g. set a templateName in the action handler like ctx.meta.templateName = "index". And check this property in the onAfterCall as well. Otherwise just return the data without templating.