Express
Express.js is arguably the most popular web framework for Node.js
A typical app structure for express celebrates the notion of routes
and handlers
, while views and data are left for interpretation (probably because the rise of microservices and client-side apps).
So an app structure may look like this:
While routes.js
glues everything together:
Unlike React Native, you could dynamically load modules here. However, there's still a need for judgement when constructing the routes (app.get/post
part).
Using hygen
let's see how we could build something like this:
Since we've been through a few templates as with previous use cases, let's jump straight to the interesting part, the inject part.
So let's say our generator is structured like this:
Then inject_handler
looks like this:
Note how we're anchoring this inject to before: "module.exports = app"
. If in previous occasions we appended content to a given line, we're now prepending it.