Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Created August 17, 2013 04:44
Show Gist options
  • Save mikermcneil/6255295 to your computer and use it in GitHub Desktop.
Save mikermcneil/6255295 to your computer and use it in GitHub Desktop.
Using custom express middleware in Sails.js circa v0.9.3

Custom middleware is a legacy option, since most times you just want to use policies.

HOWEVER! There are times you want to use sails for quick/dirty things you would normally use express for (but you already have sails around, w/e). More pertinently, if you want middleware to run before the static files from your assets folder are served, policies won't let you do that.

Here's an example of using the BasicAuth middleware for everything:
// Put this in `config/express.js`
module.exports.express = {
  customMiddleware: function (app) {
    app.use(require('../node_modules/sails/node_modules/express').basicAuth('balderdash', 'wickywocky'));
  }
};

Keep in mind this technique only works with Express (and your static files are only available that way anyhow)

@saroj990
Copy link

Express is not found in ../node_modules/sails/node_modules/express

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment