How to set up Stormpath as middleware in Sails.js

What's the best way to implement the following code in sails.js v0.10.5? Should I handle this with a policy, and if so, how? The init () function required by Stormpath requires Express (application) as an argument. I am currently using the following code in my sails.config.http.js file as custom middleware.

customMiddleware: function (app) {
        var stormpathMiddleware = require ('express-stormpath'). init (app, {
            apiKeyFile: '',
            application: '',
            secretKey: ''
        });
        app.use (stormpathMiddleware);
    }
+3


source to share


1 answer


Yes, this is the preferred way to include custom Express middleware with Sails if it does more than just processing the request (as in your case, where .init

required app

). For simpler cases where you want to implement custom middleware that simply handles requests, you can add a handler to sails.config.http.middleware

, and also add the handler name to the array sails.config.http.middleware.order

. See Default Comments in config/http.js

for an example using myRequestLogger

.



Also note that the key $custom

in the array sails.config.http.middleware.order

indicates where the code will run customMiddleware

, so you can change the order if necessary.

+2


source







All Articles