amberv amberv - 17 days ago 5
Node.js Question

Using app.use to set up some routes in sails.js

First of all, the context. I'm using agenda to schedule tasks in my sails.js app. Agenda starts in a hook, after orm and some other hook have finished. So far everything is good. Then I discovered agendash, a web interface to manage agenda tasks. And I don't manage to make it work with sails.js.

The problem is in following. This is how agendash should be used (from the doc):

var express = require('express');
var app = express();

// ... your other express middleware like body-parser

var Agenda = require('agenda');
var Agendash = require('agendash');

var agenda = new Agenda({mongo: 'mongodb://127.0.0.1/agendaDb'});
app.use('/agendash', Agendash(agenda));


And I can't find where I should put this. As I said, agenda is initialized in a hook, and then I save it as
sails.agenda
. So the only thing I really have to do is

app.use('/agendash', require('agendash')(sails.agenda))


But I'm not sure how I could add a new route like this, outside of
routes.js
(I cannot refer to
sails
in that file, it's undefined), and then protect this route with some policies (I want agendash to be available only to admin).
If I get it right, this line should be run only once, so I cannot put it in
config.http
as middleware. But otherwise the router of sails.js will overwrite the route (at least if I put
sails.hooks.http.app.use('/agendash', require('agendash')(agenda))
in a hook, the route is not exposed).

So what should I do to make it work?

Answer

Ok, I found the solution by using the customMiddleware in config/http.js where I could do app.use, protect the route by middlewares and eventually call Agendash(agenda)(req, res, next). For this to work I had to move agenda initialization out of the hook and put it inside customMiddleware. This is what my code looks like:

    customMiddleware: function(app){
        var Agenda = require("agenda");
        var Agendash = require('agendash')
        var agenda = new Agenda({db: {address: 'localhost:27017/dbName', collection: 'collectionName'}});
        //protect the route with several policies
        app.use('/agendash', function(req, res, next){
            require('../api/policies/policy1')(req, res, function(){
                require('../api/policies/policy2')(req, res, function(){
                    require('../api/policies/policy3')(req, res, function(){
                            Agendash(agenda)(req, res, next)
                        }
                    )
                })
            })
        });
        sails.agenda = agenda; //saving the reference
        agenda.on('ready', function(){
            var eventsToWaitFor = ['hook:orm:loaded', 'hook:email:loaded'];//if need to wait for some events before starting tasks
            sails.after(eventsToWaitFor, function(){
                require('./../api/services/jobs')(agenda);//loading all the jobs
                agenda.on('fail', function(err, job){
                    //log error here
                });
                agenda.every('10 minutes', 'task1');//schedule tasks
                agenda.every('10 minutes', 'task2');
                agenda.start();//start agenda
            });
        })
    }
Comments