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,...