Edit Page

sails.registerActionMiddleware()

This feature is still experimental.

This method is still under development, and its interface and/or behavior could change at any time.

Register a new action middleware function that will be applied to actions with the specified identities.

sails.registerActionMiddleware(actionMiddlewareFns, actionIdentities);

Action middleware functions are essentially policies that you declare programmatically (rather than via sails.config.policies). In fact, policies are implemented under-the-hood using action middleware. The registerActionMiddleware() method is mainly useful in custom hooks as a way of adding new policies to an app.

Usage

  Argument Type Details
1 actionMiddlewareFns or One or more middleware functions to register. Action middleware (like policies) must be functions which accept req, res and next arguments.
2 actionIdentities An expression that indicates the action or actions that the action middleware should apply to. Use * at the end for a wildcard; e.g. user/* will apply to any actions whose identities begin with user/. Use a ! at the beginning to indicate that the action middleware should NOT apply to the actions specified by the expression, e.g. !user/foo or !user/*. Multiple identity expressions can be specified by separating with a comma, e.g. pets/count,user/*,!user/tickle

The actionIdentities argument expects the identities to be expressed as if they were standalone actions. To apply action middleware to actions inside of a controller file (e.g. UserController.js), simply refer to the lower-cased version of the filename without "Controller" (e.g. user).

Example

As an example of action middleware that might be applied in a custom hook, imagine a page view counter (this code might be added to the initialize method of the hook):

// Declare a local var to hold the number of views for each URL.
var pageViews = {};

// Register middleware to record each page view.
sails.registerActionMiddleware(

  // First argument is the middleware to run
  function countPage (req, res, next) {

    // Initialize the page counter to zero if this is the first time we've seen this URL.
    pageViews[req.url] = pageViews[req.url] || 0;

    // Increment the page counter.
    pageViews[req.url]++;

    // Add the current page count to the request, so that it can be used in other middleware / actions.
    req.currentPageCount = pageViews[req.url];

    // Continue to the next matching middleware / action
    next();
  },

  // Second argument is the actions to apply the middleware to.  In this case, we want the
  // hook to apply to all actions EXCEPT the `show-page-views` action supplied by this hook.
  '*, !page-view-hook/show-page-views'

);

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Sails logo
  • Home
  • Get started
  • Support
  • Documentation
  • Documentation

For a better experience on sailsjs.com, update your browser.

Sailsconf 2022 June 22 - 24: Learn more at the Sailsconf website

Tweet Follow @sailsjs

Documentation

Reference Concepts App structure | Upgrading Contribution guide | Tutorials More

Reference

  • Application
    • Advanced usage
      • Lifecycle
      • sails.LOOKS_LIKE_ASSET_RX
      • sails.getActions()
      • sails.getRouteFor()
      • sails.lift()
      • sails.load()
      • sails.lower()
      • sails.registerAction()
      • sails.registerActionMiddleware()
      • sails.reloadActions()
      • sails.renderView()
      • sails.request()
      • sails.getBaseUrl()
    • sails.config.custom
    • sails.getDatastore()
    • sails.getUrlFor()
    • sails.log()
  • Blueprint API
    • add to
    • create
    • destroy
    • find one
    • find where
    • populate where
    • remove from
    • replace
    • update
  • Command-line interface
    • sails --version
    • sails console
    • sails debug
    • sails generate
    • sails inspect
    • sails lift
    • sails new
  • Configuration
    • sails.config.*
    • sails.config.blueprints
    • sails.config.bootstrap()
    • sails.config.custom
    • sails.config.datastores
    • sails.config.globals
    • sails.config.http
    • sails.config.i18n
    • sails.config.log
    • sails.config.models
    • sails.config.policies
    • sails.config.routes
    • sails.config.security
    • sails.config.session
    • sails.config.sockets
    • sails.config.views
  • Request (`req`)
    • req._startTime
    • req.body
    • req.cookies
    • req.fresh
    • req.headers
    • req.hostname
    • req.ip
    • req.ips
    • req.isSocket
    • req.method
    • req.options
    • req.originalUrl
    • req.params
    • req.path
    • req.protocol
    • req.query
    • req.secure
    • req.signedCookies
    • req.socket
    • req.subdomains
    • req.url
    • req.wantsJSON
    • req.xhr
    • req.accepts()
    • req.acceptsCharsets()
    • req.acceptsLanguages()
    • req.allParams()
    • req.file()
    • req.get()
    • req.is()
    • req.param()
    • req.setLocale()
    • req.setTimeout()
    • req.host
  • Response (`res`)
    • res.attachment()
    • res.badRequest()
    • res.clearCookie()
    • res.cookie()
    • res.forbidden()
    • res.get()
    • res.json()
    • res.jsonp()
    • res.location()
    • res.notFound()
    • res.ok()
    • res.redirect()
    • res.send()
    • res.serverError()
    • res.set()
    • res.status()
    • res.type()
    • res.view()
    • res.negotiate()
  • Waterline (ORM)
    • Datastores
      • .driver
      • .manager
      • .leaseConnection()
      • .sendNativeQuery()
      • .transaction()
    • Models
      • .addToCollection()
      • .archive()
      • .archiveOne()
      • .avg()
      • .count()
      • .create()
      • .createEach()
      • .destroy()
      • .destroyOne()
      • .find()
      • .findOne()
      • .findOrCreate()
      • .getDatastore()
      • .removeFromCollection()
      • .replaceCollection()
      • .stream()
      • .sum()
      • .update()
      • .updateOne()
      • .validate()
      • .native()
      • .query()
    • Queries
      • .catch()
      • .decrypt()
      • .exec()
      • .fetch()
      • .intercept()
      • .limit()
      • .meta()
      • .populate()
      • .skip()
      • .sort()
      • .then()
      • .tolerate()
      • .toPromise()
      • .usingConnection()
      • .where()
    • Records
      • .toJSON()
  • WebSockets
    • Resourceful PubSub
      • .getRoomName()
      • .publish()
      • .subscribe()
      • .unsubscribe()
    • sails.sockets
      • .addRoomMembersToRooms()
      • .blast()
      • .broadcast()
      • .getId()
      • .join()
      • .leave()
      • .leaveAll()
      • .removeRoomMembersFromRooms()
      • sails.sockets.id()
    • Socket client
      • io.sails
      • io.socket
      • SailsSocket
        • Methods
        • Properties
      • io.socket.delete()
      • io.socket.get()
      • io.socket.off()
      • io.socket.on()
      • io.socket.patch()
      • io.socket.post()
      • io.socket.put()
      • io.socket.request()

Built with Love

The Sails framework is built by a web & mobile shop in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects. Naturally we open-sourced it. We hope it makes your life a little bit easier!

Sails:
  • What is Sails?
  • Community
  • News
  • For business
About:
  • Our company
  • Security
  • Legal
  • Logos/artwork
Help:
  • Get started
  • Documentation
  • Docs
  • Contribute
  • Take a class

© 2012-2021 The Sails Company. 
The Sails framework is free and open-source under the MIT License. 
Illustrations by Edamame.