Writing the application logic

Probably, the most complex aspect of developing a web application is the controller as this is where all the logic is implemented.

In our example app, the controller will be responsible for connecting the model to the view; in other words, the controller will call a function from the model, receive some data, structure it in the correct format, and send it to the browser.

In the previous section, we structured the router dividing each feature of the app and calling a different function for each feature. In the controller, we need to implement these functions. As an example, let's write the controller function that updates the position of an existing note. In the router, we defined this function as updateNote(), so we can implement it as follows:

controller.updateNote = function (req, res, model) {
    var note = new Object();
    note.id = req.params.id;
    note.xpos = req.body.xpos;
    note.ypos = req.body.ypos;
    model.updateNote(note, function (success, result) {
        if (success) res.json({
            status: 'OK'
        });
        else res.end({
            status: 'Error'
        });
    });
}

There are a few important concepts in this code, such as the params variable that is accessible from the req object. The req object represents the HTTP request that has been received from the server, while the params attribute contains the data that has been passed via the URL, in this case, id.

Suppose we sent a request to the following URL:

POST /note/note3

In this case, the req.params.id will have the value called note3. The request object also contains the data that is passed through the body of the HTTP request. In this case, we pass the new position of the note through the request body, so we can access these two values through the req.body.xpos and req.body.ypos variables. These two variables together with id are combined together in a unique structure and object, and this object is then passed to the model that updates the database. Once the model function completes, we send the response back to the client in the JSON format:

{
    "status" : "OK"
}

This is an example of the JSON document that the controller sends back to the client if the operation succeeds. It is important to send some feedback back to the client, so the user can check if the operation fails for some reason.

By now, you should have a basic understanding of how the controller works: it parses the data received by the client, it passes the data to the model, it waits for a response, and it sends the response back to the client.

Note

For brevity, I'm not writing the entire code of the controller as writing these scripts goes beyond the goal of this book. The important thing is that you get a basic understanding of how to structure the code in an appropriate manner. You can, of course, view the complete file in the book code.

Now that we've got the router and controller up and running, let's turn to the piece of software that actually interacts with RethinkDB: the model.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.172.50