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.
To follow the tutorial, you need to have Node and NPM (Node Package Manager, best comparable with Nuget) installed.
With Node and NPM installed, you also may want to install Mongodb.
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.
Let’s go ahead and create this API.
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.
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.
Now let’s open up package.json and remove the jade dependency.
We’ll add Mongoose, a very *very* nice ORM for Mongo.
This will install the dependencies (Express and Mongoose).
Create a folder ‘models’ in the server dir and name it Member.js.
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.
Next create a tests folder in the server dir.
Create a file named 0-connection.js (that’s my own convention).
var Member = require(‘../models/Member’) this is were we import the Member class library.
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.