Marshall Thompson
Marshall Thompson

Reputation: 945

How to use custom route middleware with Sails.js? (ExpressJS)

I've just unpacked a fresh copy of the Node framework Sails.js. It is built on Express 3. In the /config/routes.js file is this comment:

/**
 * (1) Core middleware
 *
 * Middleware included with `app.use` is run first, before the router
 */


/**
 * (2) Static routes
 *
 * This object routes static URLs to handler functions--
 * In most cases, these functions are actions inside of your controllers.
 * For convenience, you can also connect routes directly to views or external URLs.
 *
 */

module.exports.routes = { ...

In the same config folder I have created the file called is_ajax.js.

// Only run through API on ajax calls.
module.exports.isAjax = function(req, res, next){
  if (req.headers['x-requested-with']) {
    // Allow sails to process routing
    return next();
  } else {
    // Load main template file
    // ...
  }
};

My intended purpose is to make non-Ajax GET requests all load the same template file so my CanJS application can set up the application state based on the URL (so my javascript application is properly bookmark-able).

I would like to run that script as middleware. Can somebody please show me how to use app.use() in this case to have the is_ajax.js script run before other routing?

I'm guessing it's something like

var express = require('express');
var app = express();
app.use( require('./is_ajax') );

Only when I do the above, it tells me that it can't find the express module. I've verified that express is a module inside Sails' node_modules. Is there another syntax for loading it? I'd rather not have to install a second copy of express alongside sails. Is there a way to access the original Sails/Express app instance?

Upvotes: 9

Views: 13807

Answers (3)

TianxiLiu
TianxiLiu

Reputation: 367

To add compress middleware of express, I find this thread and

sails-middleware-example-issue are very usefull.

  1. install express local: npm install express
  2. load express: var exp = require('express')
  3. add customMiddleware in $app_dir/config/local.js
express: {
    customMiddleware: function (app) {
      console.log("config of Middleware is called");
      app.use(exp.logger());
      app.use(exp.compress());
      app.use(function (req, res, next) {
        console.log("installed customMiddleware is used");
        next();
      })
    }
  }

Upvotes: 4

rishabhmhjn
rishabhmhjn

Reputation: 1079

I had the same issue of figuring out how to make use of middlewares. They are basically defined in the config/policies.js.
So, if you want to use middlewares(aka policies) like old style, you can do the following (this may not be the nicest way):

// config/policies.js
'*': [ 
  express.logger(),
  function(req, res, next) {
    // do whatever you want to
    // and then call next()
    next();
  }
]

However, the real sailjs way is to put all such policies in api/policies/ folder

Upvotes: 10

sgress454
sgress454

Reputation: 24958

You can use policies to achieve this. Save your isAjax function as isAjax.js under your api/policies folder, and change it to just use module.exports instead of module.exports.isAjax. Then in your config/policies.js file, you can specify which controllers/actions to apply the policy to--to run isAjax for every route, just do:

'*':'isAjax'

in that file.

Upvotes: 19

Related Questions