hapi icon indicating copy to clipboard operation
hapi copied to clipboard

Organizing Routes (and other project setup advice)

Open blissdev opened this issue 13 years ago • 7 comments

Wasn't sure where to ask this, so I hope here is ok.

I'm trying to figure out how to setup routes in a way that is organized and easily testable. I was going with the idea of having a route per file, but I'm concerned that this will cause bloat by including the same dependencies over multiple routes and also I'm having trouble injecting dependencies for tests.

How does Wal-mart setup routes? Are there any other best practices in relation to setting up a Hapi project? Thanks!

blissdev avatar Jun 18 '13 02:06 blissdev

We use the Hapi Composer, and each endpoint ends up being its own npm module which we keep on an internal git repo.

Once I get to a computer I will link you to the Composer documentation. :)

On Jun 17, 2013, at 7:32 PM, Jordan Arentsen [email protected] wrote:

Wasn't sure where to ask this, so I hope here is ok.

I'm trying to figure out how to setup routes in a way that is organized and easily testable. I was going with the idea of having a route per file, but I'm concerned that this will cause bloat by including the same dependencies over multiple routes and also I'm having trouble injecting dependencies for tests.

How does Wal-mart setup routes? Are there any other best practices in relation to setting up a Hapi project? Thanks!

— Reply to this email directly or view it on GitHub.

nvcexploder avatar Jun 18 '13 03:06 nvcexploder

An addendum question: We use Postgres and thus need to include the pg library in many of our requests. Correct me if I'm wrong, but I don't want to create new pg connections in the individual routes, I probably want a shared pg object that I share in between them. Does Composer allow this?

blissdev avatar Jun 18 '13 05:06 blissdev

Node caches requires() calls so no need to worry about loading the same deps multiple time in the same process.

hueniverse avatar Jun 18 '13 05:06 hueniverse

@hueniverse Sorry, I should have been more specific. pg requires that I connect to my db, and thus I'd like to share that "connection" between requests. Does that makes sense?

blissdev avatar Jun 18 '13 05:06 blissdev

So create the connection in one file and require it from everywhere you need access to it. Node will create a singleton pattern for you automatically.

hueniverse avatar Jun 18 '13 05:06 hueniverse

@hueniverse Thanks your patience and helpfulness. One last thing, do you know of any open source hapi projects that exist for which I could read the code? Looking for something slightly bigger than an example to see some organization/setup stuff on.

blissdev avatar Jun 18 '13 05:06 blissdev

We're working on it. I need to finish migrating postmile.

hueniverse avatar Jun 18 '13 16:06 hueniverse