Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 689
Marshall Thompson Avatar asked Aug 31 '13 13:08

Marshall Thompson


People also ask

How do I use middleware in sails JS?

Adding middleware To configure a new custom HTTP middleware function, add a middleware function as a new key in middleware (e.g. "foobar"), then add the name of its key ("foobar") in the middleware. order array, wherever you'd like it to run in the middleware chain.

Why does node js need middleware?

The main purpose of the middleware is to modify the req and res objects, and then compile and execute any code that is required. It also helps to terminate the request-response session and call for the next middleware in the stack.

What is the use of app use in node JS?

use is a way to register middleware or chain of middlewares (or multiple middlewares) before executing any end route logic or intermediary route logic depending upon order of middleware registration sequence. Middleware: forms chain of functions/middleware-functions with 3 parameters req, res, and next.

What is the use of Next in Express JS?

The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware. Middleware functions can perform the following tasks: Execute any code. Make changes to the request and the response objects.


2 Answers

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.

like image 119
sgress454 Avatar answered Nov 06 '22 10:11

sgress454


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

like image 43
rishabhmhjn Avatar answered Nov 06 '22 11:11

rishabhmhjn