Question

I'm trying to separate my mongodb code in a nodejs express app and am having troubles understanding how to shuffle things around.

here's a gist of what I have https://gist.github.com/759446

I've dumbed it down to almost nothing in the middleware. When I start the server, res.myvar is correctly set to "object" in the first request. Every subsequent request fails with res.myvar being "undefined".

I just want to stick the db code in a single place and then be able to use it in my various routes. Am I going about this wrong? All the examples on the mongodb nodejs driver page just make db queries etc. I'm not finding much integrating the whole thing in an express app.

Was it helpful?

Solution

What I had originally was opening new db connections on every request. I've edited the gist and left the old stuff commented out.

It works now.

Many thanks to Ciaran's blog post http://howtonode.org/express-mongodb It's kinda old but still helped.

OTHER TIPS

I am writing node-fourm, I have the same problem at beginning. There is a db folder and controller folder in node-fourm, they are seperated now, and I can define method for each collection now. Check the code for detail.

I wrote mongoskin to make it possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top