Well, as mentioned in the before article (http://chilion.nl/setting-up-a-nodejs-api-with-express/) you might want to use a dedicated Routes file before your serve.js gets out of control.

Let me guide you trough this one;

First of all, lets create a new folder called routes

mkdir routes
cd routes

Then, create anew file called routes.js and fill it with the following code:

module.exports = (function() {
    var router = require('express').Router();
      // Here we will put our own routes.
    return router;
})();

As you can see, we do nothing more than initialise the router and return it with all the routes in place.

Lets require it in our serve.js file.

Change the routing part to the following, for your convenience I’ve put in the complete serve.js file to see the differences;

// Get all the packages that are needed for the API
var express    = require('express');        
var bodyParser = require('body-parser');

// Set the port we will be using for the API
var port = process.env.PORT || 80 

// Define the API object.
var api = express();

// Configure what our API should use from the bodyParser package
api.use(bodyParser.urlencoded({
	extended:true
}));

api.use(bodyParser.json());

// The routing part of our API, if extending the API it should be in it's own routing.js file.
var routes = require('./routes/routes.js');
api.use('/api', routes);

// And now, let the fun start; or our server of course.
api.listen(port); 

// And log it away
console.log('Well, looks like you found Lucky Luke. ' + port);

The serve.js file now will know where to find the routes and that it should use that file.
Lets create our own routes shall we?

Add the following route:

  // A route to check if everything is working like it should
	router.get('/', function(req, res) {
	    res.json({ message: 'Take a beer or 2, you just found me.' });   
	});

Also, lets add another one that displays that bullshit is bad for ya. Its a quote by George Carlin, so make sure to make the url /george

Now, lets check it out, if all went well, you will see that the routes are just working fine, and if they don’t, well, you did something wrong. Don’t hesitate to ask in the comments, I will try to help you out.

Your routes file by now:

module.exports = (function() {
    var router = require('express').Router();

    // Another route, just to make sure it really really works.
	router.get('/george/', function(req, res) {
	    res.json({ message: 'Its all bullshit folks, and its bad for ya. - George Carlin' });   
	});

    // A route to check if everything is working like it should
	router.get('/', function(req, res) {
	    res.json({ message: 'Take a beer or 2, you just found me.' });   
	});

    return router;
})();

Thats all about routing so far, I will extend it later with post, delete, put etcetera. But first, I’m going to use MySql!

For the complete code, check Github with this Branch