+1 On Saturday, February 2, 2013 12:24:07 AM UTC-8, Stephen Bartell wrote: > > > > On Friday, February 1, 2013 8:10:35 AM UTC-8, Thomas Gray wrote: >> >> Hi guys >> >> Let me start out here with a disclaimer. I am a newbie to programming in >> Javascript. It's syntax is acceptable, so I have no problem with that - >> it's just on the structuring of Javascript especially within the Async >> context of nodeJS. >> >> I am at the moment trying to write a really simple RESTful API in NodeJS >> using Mongo as a data source. I'm using express to handle the HTTP >> component and the MongoDB library (via NPM) to handle my interaction with >> Mongo. I am having some real troubles with how to properly structure my >> application. You can view it as it stands here: http://pastie.org/6013517 >> >> As you will see, I am calling the user collection, then finding a user, >> then creating a new login token, then updating the user with that login >> token, then sending that login token back to the browser. All within one >> crazy nested method. I've been programming in non-async languages for a >> while, and I guess I am failing to understand the concept of how to program >> asynchronously. The best I can come up with is to have a User object that >> just has methods that are called within the callback of each component. >> >> Looking at the code I have given above - how would I better structure >> this to remove the levels of nesting, but still not have it as a procedural >> object (ie. one method calls the next which calls the next and so on). >> >> Maybe i'm just not understanding how to program in this environment? >> > > I think you're totally getting it. JS is callback driven. You just need > to find a method of organizing the code that makes most sense to you. > Maybe you'll adopt one of the libs that are proposed in this post, maybe > you'll use events, maybe you'll make your own lib. Maybe you wont give a > damn and just use nested callbacks. > > Theres no perfect style to all of this. And to be honest, if what you have > gets the job done and makes the client happy, then you have a win. > > Personally, I think that struggling through all this async shit on your > own without using helper library is the only way you're going to grok > whats really going on beneath the covers. Once you really get comfortable > with how it works, then you can give into being lazy (in a non-insulting > sense) and just use a lib, or write your own. > > I believe in learning by immersion. Read code, lots of code. Especially > the parts where objects are initiallizing themselves. Off the top of my > head, look at node_redis and follow. There are so many others too. I > usually dig through the package.json dependencies and learn how those work. > It a never ending rabbit hole of code to learn from. > > One thing i did notice in your pastie is that you're requiring crypto > every time you call hashpassword. Just call it once at the top of your > file. > > good luck grasshopper ;) > > >> >> Tom >> >> >>
-- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
