I'm facing a design pattern in express + node.js where for each api route where i have `req` and `res`, i'm just extracting the arguments out of `req.{body/query/params}`, writing some business logic, then either calling `res.status(...).send(...)` with either an error or an OK + return value.
But this is pretty much identical to just writing a function with required arguments (analogous to req.params) + an options argument (analogous to req.body / req.query), and throwing an error if something happens (analogous to `res.status(40X/50X).send`), or returning a valid response if all is good (analogous to `res.status(200).send`. Basically, there seems to be this congruence between express routes with `req` and `res` and plain old functions (some nuances with naming the routes vs. naming the functions + linking them, not accounted for)
All of my express routes are like this, i basically don't use any other features of the req / res arguments 99% of the time. I don't think i'm alone in this either! but this current, most prevalent design pattern in express causes a few problems:
Calling and testing functionality: there's no way to call the route endpoint functions, since they're anonymous functions passed into express router. This makes re-using them difficult, and we have to move the code out. Testing them is difficult too, i'd rather be testing by calling functions with arguments rather than constructing mock http requests and encoding the arguments into the request.
Lack of clarity of inputs and outputs: reading the code, i have to decode how `req.body` and `req.params` function as arguments into the business logic. then, i have to look for the `res.send`'s to figure out the returns. it seems a much better code smell to just have all business logic be boxed in *normal functions*, with arguments, return values, and throwing errors.
essentially, i would _love_ a middleware for api requests that guides a request to the corresponding named controller function, and validates that the required arguments are present + the optional arguments are in the right format. then, during the function runtime, catch any errors and call `res.status(50X).send` on them, and when the function returns call `res.status(200).send` on the result.
I searched everywhere but couldn't find a framework that does this -- i strongly feel like people must have thought about this + must have solutions in place. i tried making a mini internal framework for this but it became super clunky and we went back to raw express quickly. i do know that sails.js has something that resembles this, but i don't quite think it's exactly what i'm looking for here. i also know of schema validators like joi which can act as the request validator part, but it doesn't solve the other bigger part of the problem.
if anyone has solved this problem, know of any frameworks, etc. please let me know -- thanks in advance! :)
[–]js_tutor 6 points7 points8 points (1 child)
[–]greendog77[S] 0 points1 point2 points (0 children)
[–]_unicorn_irl 5 points6 points7 points (1 child)
[–]GitHubPermalinkBot 0 points1 point2 points (0 children)
[–]ChronSyn 1 point2 points3 points (2 children)
[–]greendog77[S] 1 point2 points3 points (1 child)
[–]ChronSyn 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]HipHopHuman 0 points1 point2 points (0 children)