Routing

Now that we have a menu, we need to be able to route our application and display the items that correspond to the menu. This is where slugs are very convenient. The proposed solution is a route \* that intercepts all routes. It is a single entry point for the application. The path is then decomposed and only the last segment is used as it always corresponds to an item that we want to display. This is not the most robust solution, but it can easily be used to render a static website by calling all the routes generated by the menu.

    app.get('*', (req, res, next) => {
        const slug = req.path.split('/').slice(-1)[0];
        r.item(branch, slug).then(data => {
            r.breadcrumb(branch, data.item[0]._qname).then(d => {
                data.breadcrumb = d;
                res.data = data;

                if(data.item[0]._statistics['a:category-association_INCOMING'] > 0){
                    r.relatives(branch, data.item[0]._qname).then(d => {
                        data.items = d;
                        res.data = data;
                        next();
                    }).catch(() => {});
                } else {next();}    

            }).catch(() => {});;
        }).catch(() => {});
    }, breadcrumbParser, (req, res, next) => {
        res.render('page', { data: res.data });
        //res.json(res.data);
    });

We recognise the same middleware architecture. We find the slug by splitting the request's path on each /. The string.split() method produces an array that we slice from the end and we recuperate the first item of the newly returned array.

Then we read a node exactly as we did for the menu and with the acquired qname we do two things:

  1. Generate the breadcrumb for the item
  2. Get all the items that belong to a category if we are dealing with a category and if this category has items. We make sure that we only do a query if there is INCOMING relations as we do not want to do a request just for OUTGOING relations that we will not display. (The category could be a subcategory)
    All this the data is added to the response object and then we call next() to pass the data to the next middleware.

results matching ""

    No results matching ""