I’m intrigued by node – partly because of all the recent hype and ‘cool kid’ factor – but mostly because I like the idea of having a single language context on both client and server. Plus, the overall programming style for authoring Web apis feels similar to the style I enjoyed in sinatra. At any rate, in an effort to better understand node, I’m porting RestBugs from ASP.NET Web API to node.
Here’s where I’m at currently – https://github.com/howarddierking/RestBugs/blob/NodeJS/RestBugs-Node/app.js
It feels like I’m actually close to a completely working application – however, there are a couple things (1 thing really) that’s keeping it from working, and before I go write a bunch of code to do something totally hacky, I would rather just ask you good people. Here’s a sample of the type of code in question.
Here, I’m getting a JSON doc from MongoDB based on the id that was sent from my form and I want to execute a function on it. That function is declared as a privileged method on a custom JavaScript object.
Now, you probably already know that the route handler code above won’t work. Why? Because MongoDB is giving me back a JSON object and I need to work with it as a Bug object. If I was using JSON instead of form URL encoding for my POST input, I would likely run into the same problem on that side of the fence as well. This leads me to my question:
What is the right way to think about moving between JSON input/output objects and “domain” objects?
My brain keeps telling me that my desire to map properties from one object to another (via my own code or a library) is wrong in a language like JavaScript, but I also came across some posts (http://erik.eae.net/archives/2005/06/06/22.13.54/) that seem to indicate that tricks like swapping out the prototype may be an even worse idea.