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

In the previous article we’ve set up the web application skeleton with Express for the reMember application and we wrote a first test to see if we can actually save an object to the database with Mongoose.

In an Express generated app, ‘app.js’ is the starting point of the application. (Like Global.asax or WebApiConfig in an ASP.NET MVC app).

Since the goal of this article is to write an api, we can clean it up a little.

  • We will not be using jade at this time, so we can clean out references to the view.
  • We can also clean out the ‘user’ lines because we are not going to use these.
  • We will comment out the routes as well and create a new default route to an index.html file

Now create an index.html file in /public:

And start up the server with node app.js
You should now be able to browse to localhost:3000 and see the index.html.


But what if you don’t and you see something like this:

Then you probably forgot to close a curly brace or a quote. Stop node (ctrl+c), correct the changes and start again.
But wait, there’s an easier method.
With nodemon it is possible to debug changes on the fly.
Install it globally with npm install -g nodemon.

Now start app.js with nodemon app.js

Everytime you change and save a file the node server is automatically restarted. It crashed when something is from and it starts running as soon as you correct your mistakes:


Take a look at http://localhost:3000 now:


So that works.

Are we going to write that API or what?

Yes we are.
Delete all the files in the routes folder.
Create new file called api.js

I’ve written the C#/.NET equivalents in the comments for convenience.

The Mongoose ‘find’ method signature is written in their API. For example:

The result of the query will be put in ‘docs’, which is always the 2nd parameter.

The Mongoose API docs are a very good read and helped me enormously to understand how it works.

Mapping the mongoose functions to a route in app.js

We now need to map the exports.members function to the route /members.
This happens in app.js.

Restart node (or don’t of you’re running nodemon) and browse to http://localhost:3000/members.


I’ve added some Grimm characters.

Tip: use Postman to test the API.

The github repo for reMember is here.

Next time we’ll start building the frontend with Angularjs. (The ‘A’ in the MEAN stack).

ASP.NET Web API, Angularjs and MongoDb part 5

This is the sequel to this.

You can find the finished application here:


When I was creating this I was really confused about ‘the double MVC’ setup. The one that comes with ASP.NET and the one that comes with Angularjs.

It felt like what I was doing was bloathed and wrong.

I decided to start again from scratch, but this time with Node as the backend.