Nodejs and the MEAN stack for the .NET developer part 1

Let’s have some fun with Nodejs.

Personally I enjoy coding a lot in Vim, even more than in Visual Studio or Webstorm. Why, you might ask. Well, because Vim is extremely lightweight and totally configurable.

Also, I like working on a Linux box, because working from the command line is a huge productivity boost. It takes some time to get used to though.

I blogged about my Linux setup on my other blog. I also spent some time getting to know Nodejs here.

Let’s create a Member Administration Tool (reMember) with Node, Express and Mongo

To follow the tutorial, you need to have Node and NPM (Node Package Manager, best comparable with Nuget) installed.

Guidelines are here.
With Node and NPM installed, you also may want to install Mongodb.

Functional requirements for the reMember
The Member Admin tool contains Members for a club or whatever. You can give the members a login, so that they can update their own information. You, as an admin, get to see all the member info, while the members only can see their own nformation.

Designing the API

/members/1
[table]
Method,Result
GET,show member with id 1
PUT,if exists update member
DELETE,remove member
POST,will not work
[/table]

/members
[table]
Method,Result
GET,lists all members
PUT,bulk update members
DELETE,deletes all members
POST,creates new members
[/table]

Let’s go ahead and create this API.

npm -g install express
express reMember
//some feedback is displayed
cd reMember

What happened?
Express is a web application framework for Node, a bit like ASP.NET MVC or Nancy in .NET. (Or Sinatra for the Ruby folks). We need to install that on top of Node, with NPM. We add the -g switch because we want to install it globally.

With Express it is easy to create a web application skeleton. After executing ‘express reMember’ a directory is created like this:

▾ public/
  ▸ images/
  ▸ javascripts/
  ▸ stylesheets/
▾ routes/
    index.js
    user.js
▾ views/                                                                                                                
    index.jade
    layout.jade
  app.js
  package.json

Jade is a bit like Razor and a very elegant markup language. However, I just want to code an API and am not interested in rendering views at this time. So I’m going to delete the views folder.
I also want the server in a separate folder, for seperation of concerns.

This is the result:

/home/jacqueline/apps/reMember/                                                                                      
▾ public/
  ▾ images/
  ▾ javascripts/
  ▾ stylesheets/
      style.css
▾ server/
  ▸ routes/
    app.js
    package.json

Now let’s open up package.json and remove the jade dependency.
We’ll add Mongoose, a very *very* nice ORM for Mongo.

{
  "name": "reMember",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.4.7",
  "mongoose": "*"
  }
}

Now cd into the server folder and execute npm install
This will install the dependencies (Express and Mongoose).

The result is:

/home/jacqueline/apps/reMember/
▾ public/                      
  ▸ images/                    
  ▸ javascripts/               
  ▸ stylesheets/               
▾ server/
  ▾ node_modules/
    ▸ .bin/
    ▸ express/
    ▸ mongoose/
  ▾ routes/                                                                                                             
      index.js
      user.js
    app.js
    package.json

Create the model

Create a folder ‘models’ in the server dir and name it Member.js.

var mongoose = require('mongoose');

var Schema = mongoose.Schema;
var memberSchema = new Schema({
  nickname : String,
  email: String,
  firstName: String,
  lastName: String,
  password: String,
  education: [{ title: String, institute: String, certification: Boolean}]
});is 

module.exports = mongoose.model('Member', memberSchema);

Now, if you ‘require’ this file in e.g. app.js, you can use all the methods of Member.
Very much like ‘using’ in .NET.

Let’s test this already!

First install Mocha to test this. Install Mocha globally by running npm install -g mocha
Then add the Should package in the package.json:

{
  "name": "reMember",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.4.7",
    "mongoose": "*",
    "should" : "*"
  }
}

Run npm install.

Next create a tests folder in the server dir.
Create a file named 0-connection.js (that’s my own convention).

var should = require('should');
var Member = require('../models/Member');
var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/local');

describe('Member', function(){

    beforeEach(function(done){
        //clean the database:
        Member.remove(done);
    });

    describe('#save()', function() {
        it('should save', function(done) {
            var member = new Member({firstName: 'Florian' })
            member.save(function(err) {
                if (err) return done(err);
                //assert.equal(volunteer.firstname,'Joe');
                member.should.have.property('firstName','Florian');
                done();
            });
        });
    });
});

Run mocha 0-connection.js

$:~/apps/reMember/server$ mocha tests/0-connection.js 

  ․

  1 passing (20ms)

var Member = require(‘../models/Member’) this is were we import the Member class library.
By doing that we can create an instance of a Member class like this:
var member = new Member( {firstName: “florian” );

Mongoose is the ‘Entity Framework’ for Mongodb. When using Mongoose to define our Member model, the Member instance automatically gets methods like save, findOne and findOneAndRemove.

In the next post we’ll start writing the API.

Resources:
http://blog.mongodb.org/post/49262866911/the-mean-stack-mongodb-expressjs-angularjs-and

http://mongoosejs.com/docs/guide.html

http://visionmedia.github.io/mocha/#asynchronous-code

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: