Saturday 23 September 2017

Sharing Context from handlers to controllers

Hi Everyone -I have an express app with your conventional setup of an app.js file, routes and controller directory. Every incoming request has context values that I need in my controllers to make an API call and subsequently respond back to the end-user.Right now, its silly, I have the same code EVERYWHERE in my handlers.express.post('/install', async function(req, res) { //fetching values from context let value1 = req.value1; let value2 = req.value2 await controller.doSomething(value1, value2) }); Im still a n00b programmer, but I know having to do this in every handler and my controllers having the same freaking params seems like a DRY principle not followed.Should I write a middleware that stores the values in an object? If so how is that object then passed to my controllers?Any help would be appreciated and I hope my question is clear.

Submitted September 23, 2017 at 10:18PM by pratiks3

No comments:

Post a Comment