Backbone and REST API Part 2 - Server

on August 20th, 2015

As I promised on the first post, this is a post to explain the server side of that Backbone sample with Rest API.

Actually, as I told there, it's pretty simple: just starting a server, listening on a specific port, waiting for some HTTP methods and persist on a very small database. Let's see this step by step and then the whole file.

First the first stuff

First part, requiring dependencies:

var express     = require('express'),
    bodyParser  = require('body-parser'),
    DataStore   = require('nedb'),
    app         = express(),
    http_port   = 3000,
    usersDb     = new DataStore({filename:'users.nedb'});

Here we are calling a micro web framework called Express for helping on mapping urls and HTTP methods. Then we use body-parser to format our requests. NEDB, our little database.

After those requires, we create an instance of Express, choose a port, and load the base users.nedb.

app.use(express.static(__dirname + '/src'));
app.use(bodyParser());

Now we just config our static directory to src and ask Express to use body-parser.

OK, we are ready to start our mappings!

URL Mappings

Express URL mapping use the following syntax:

app.[HTTP_METHOD]([URL], [What will happen0]);

So the first mapping is:

app.get('/users', function(req, res) {
    // logic for getting our users from database
});

Very simple, isn't it?

Ok, now you want to receive a get parameter. Not a problem, you can see this on the second mapped URL.

app.get('/users/:user_id', function(req, res) {
    var id = req.params.user_id;
});

You just have to say that this URL will receive a parameter via :param_name, and get this on attribute params of request.

After doing your stuff, in this case just get data from database, you have respond to your requester. And this is made using the method send of response. Just like this:

app.get('/users', function(req, res) {
    res.send({
        'my' :'data'
    });
});

Knowing all this, you can now implement your POST, PUT and DELETE.

Finishing...

Now just start the server and be happy!

app.listen(http_port);

Note: this full code can be viwed and downloaded on my Github

Tags:

backbone, node, rest, javascript

by Renie Siqueira