There are tons of great tutorials on Node at Node School. The one I did was “learnyounode” which is a series of 13 exercises. It’s pretty simple, although I got annoyed a few times and grabbed the answers from learnyounode-solutions. Hey, I’m not getting graded on this or anything. It’s easy to get started:
brew install npm npm install -g learnyounode learnyounode
Coming from a Rails background, I was more interested in how you can leverage Node to create web applications. This led me to Express.js which is pretty much exactly what a Rails developer might expect if they were looking for Node on “Rails.”
Express has an app generator that will build a complete application structure in a directory. From there, you can start the application and begin customizing routes and content. It features a routing mechanism and a template language called Jade, although you can supply others if you wish. In short, to start a new Express app:
npm install express npm install express-generator -g express my-app cd my-app npm install npm start
It even starts up the webserver on the usual 3000 port. The similarities with Rails were actually a little eerie. Express isn’t a full MVC framework, though, it’s more like VC, or just V for that matter. As-is, Express gives you views and routes so it’s very easy to start creating simple static pages or pages that are transformed using JSON data objects.
For example, if I wanted to add a simple “About me” page to my base Express application, I create a route:
1 2 3 4 5 6 7 8 9
Because the route file is
about.js it’s going to respond to GET requests at
/about which is why the path is only “/”
on line 5. After that, it’s just passing a JSON object to the view template, which looks like:
1 2 3 4 5
In order to have the page render, we’ll need to wire it up to our application. Add these lines in the
relevant locations of your
var about = require('./routes/about'); app.use('/about', about);
Now you can restart the server and view the page. Not much there, but you get the idea. I left it very basic because I was more intested in…
Coming from Rails, I’ve had TDD/BDD beaten into my head so it was very hard for me to doing anything without asking myself “Where is this tested?” To answer that, you have to look at some testing frameworks. I started with expect.js which has the syntax you might expect (heh) with Rspec. However, getting it wired up in Express required a few additional bits: Mocha and superagent. Expect.js provides the language for testing, Mocha provides the framework, which is a lot like Rspec and uses “it” and “describe” blocks. Superagent is a client-side HTTP request library that is making the actual calls to your app.
Here’s how I put it all together
npm install expect.js superagent npm install -g mocha mkdir test mocha
After executing that last command, it will run mocha and report “0 passing” tests. So, let’s add one for our about page:
1 2 3 4 5 6 7 8 9 10 11 12 13
Superagent makes the get request and using expect.js we can parse the body of the response for the expected content. Note: I was getting the warning “double callback!” which may be a bug, but I’m not sure. Everywhere on the net, this seems to be the accepted syntax, so take this for what it’s worth.
Another testing method is to use Jasmine which basically does the same as above,
even with some of the same dependencies, but slightly differently. To setup, let use our
package.json the way it’s meant
to be and specify our dependencies. Add these lines to the
"jasmine-node": "~>1.14", "request": "~2.56"
npm install jasmine init
Jasmine follows Rspec a little more closely and creates a
spec directory and assumes you’ll name your tests with the
_spec extension. To test our about page using Jasmine:
1 2 3 4 5 6 7 8
One thing to note, this test will only pass if your app.js is actually running. There are ways to wrap this so that Jasmine will start up the app first before testing.
M is for Model
The last bits to add into this mix are getting Express to model data in a ORM kind of way like Rails does. Again, there are lots to choose from here: Bookshelf, Backbone, Mongoose, Persistence, Sequelize, and the list goes on.
Many of these cater to one particular database, such as Mongoose connecting to MongoDB. There’s a specific platform that wraps these together: MEAN which stands for MongoDB, Express, Angular.js, and Node.js. It’s reminiscent of LAMP.
Too Many Choices?