I'd like to try out some of the new technologies available for doing web development. I've been using php frameworks, rails and asp.net mvc for quite some time now and I saw that Node.Js provided a lot of good stuff to the table.
But I don't know how and where to begin. I setup mongodb, node.js and express.js and I played with it a bit but I'd like to know how to make the link between express.js and mongodb (I have mongoose but I don't know how to include it yet) and I'd like some brief and simple examples (unlike stuff that is in express.js docs) to see some actual usage cases.
I'd like to build some quick content management system and with a simple frontend, and I wanted to make a change and try that out. Any advice and help will be appreciated!
[edit: I already read http://howtonode.org/express-mongodb it helped but it doesn't explain much about how to do it]
Here's a get started blog post for Node.js + MongoDB. You can easily ignore the Cloudfoundry stuff. Mongoose is a layer over the basic driver, so you should probably start with the basic driver.
If you want some code / ideas to steal, take a look at railwayjs.
Start simple. Code a basic web site using vanilla node.js standard library HTTP module (
require('http');). Learn the basics of the http request and response interfaces, especially the evented interfaces for streaming data. Learn the basic fs module and how to take plain
.html files on disk and send them as HTTP responses. You can do this in a single
.js file without needing any third party npm modules. Full stop until you have that done and it makes sense to you. If something isn't clear, experiment until it is (and ask more questions here). This is straightforward, but there are still lots of ways to get confused.
Once you have that, you'll see that it's actually already pretty high-level and straightforward to write a small web site. Next add
express.js. Study the concept of
connect middleware and how the middleware chain works, including the
static middlewares. Make sure you understand why middleware often needs to be applied in a specific order and how it breaks if you apply them in the wrong order. Understand express's
dynamicHelpers, and most of the examples in the documentation, which are all very good and realistic but I agree because they are just tiny snippets they leave a lot of room for questions about the surrounding context and how things fit together entirely. Don't go nuts with complex layouts and templating stuff, just stick to the basics.
Then maybe start persisting some domain level data into simple files on the filesystem, perhaps just
.json data. This will present new challenges and help you understand more complex things in an async world. In particular, if you start having to do a series of operations like recursively ensure a directory path exists and then write a new file in the final leaf directory, you will want to look at a flow control library like async or async.js or similar.
Only then should you start adding mongoose and mongodb to your stack and replace your
fs.writeFile calls with mongoose
For me the biggest help I found when experimenting with all these awesome new technologies was looking at this sample application:
Demo App - https://github.com/madhums/nodejs-express-mongoose-demo
It shows some good usage of how to organise a node-express application, some common utilities for showing mongoose validation errors messages, stylus for css, jade views, etc. And the author appears to take requests if you think something should be added to the demo app to help others learn a new express/mongo/etc feature - I haven't asked personally but there is a check-list on the github page showing what features are completed and what's upcoming.
I would also recommend just getting your hands dirty :) Come up with a simple idea and try to build it. And when something doesn't feel right or you have a block then there's a pretty amazing and growing community out there.
Here are some tutorials not mentioned above:
Mongoskin (mongoose alternative) - http://www.hacksparrow.com/mongoskin-tutorial-with-examples.html
Exress JS tutorial: http://www.hacksparrow.com/express-js-tutorial.html
Also if you go to the github page for expressjs you will see many examples of specific express functionality - which goes into some great specifics:
Express js examples: https://github.com/visionmedia/express/tree/master/examples