Configuring the router

When you design the routes for a web application, you should think about all the features that the application should provide to the user and map these features to an HTTP request.

In our example app, the user will need to load the homepage, get a list of all existing notes, create a new note, update a note's position, and delete a note. What we should do now is create an API for our app, which creates a route for all of these actions. I've personally organized it with the following routes:

  • get (/: This route gets called when the user visits the app's homepage from his browser and is responsible for rendering the view to the screen
  • get (/notes: This route allows us to request a list of all existing notes that are stored in the database
  • post (/notes: This route is called when the user creates a new note
  • post (/note/:id: When the user moves a note, a request to this route is performed, passing the note's ID
  • delete (/note/id: Finally, this route allows us to delete a specific note from the database

Once we've mapped each feature to its route, we need to tell Node.js what it should do in response to each route; however, the application logic should be performed in the controller, so the routes file will just associate each route with the corresponding function in the controller.

As an example, let's look at the route that deletes an existing note:

app.delete('/note/:id', function (req, res) {
    notesController.deleteNote(req, res, app.get("model"));
});

The first thing we need to specify is the HTTP "verb", in this case, delete. We do this by calling the delete() function on the instance of the Express.js app, passing it the URL as the first argument. The second argument is a callback function that calls the controller's deleteNote() function.

Let's go over this once again. When the client (in our example, the browser) performs a HTTP request such as delete /note/note3, the router will call the deleteNote() function that is defined in the controller. All other routes are very similar to this one; however, there is one exception: the homepage.

When the user opens our web application in his browser, the browser automatically performs a get(/ request. What we need to do is map this request to the code that renders the homepage view. Since this doesn't require any logic, we can do this directly from the router. Take a look at the following code:

app.get('/', function (req, res) {
    app.get("model").getNotes(function (success, result) {
        res.render('index', {notes: result});
    });
});

Let's go over this code as there a few things happening. When the router receives a get(/ request, it first calls the model's getNotes() function that returns a list of all notes that are saved in the database. Then, we call the render function, passing the name of the view as the first argument and the notes' data as the second argument. When this line of code runs, the EJS view engine will insert the dynamic data into the template and will render the index.ejs view to the browser, displaying the homepage.

When you've written all the routes, the index.js file should look as this:

var notesController = require('../controllers/notes');
module.exports = function (app) {
    app.get('/', function (req, res) {
        app.get("model").getNotes(function (success, result) {
            res.render('index', {
                notes: result
            });
        });
    });
    app.get('/notes', function (req, res) {
        notesController.getNotes(req, res, app.get("model"));
    });
    app.post('/notes', function (req, res) {
        notesController.createNote(req, res, app.get("model"));
    });
    app.post('/note/:id', function (req, res) {
        notesController.updateNote(req, res, app.get("model"));
    });
    app.delete('/note/:id', function (req, res) {
        notesController.deleteNote(req, res, app.get("model"));
    });
};

Notice how we load the controller at the start of the file. In the following section, we'll look at the controller in more detail.

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

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