npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-permissions

v0.1.0

Published

Express Middleware Permissions system

Downloads

22

Readme

express-permissions

Build Status

Easy to use permissions system for Express

Install

npm install express-permissions --save

Usage

Require express-permissions and load it as middleware.

ExpressPermissions = require('express-permissions')

app.use(ExpressPermissions.middleware())

Middleware is executed in order from first to last, make sure any middleware you will need to run first is added before express-permissions.

Defining permissions

Permissions are defined by calling ExpressPermissions.add() which takes 4 arguments

  • The express app
  • The path you want to match
  • The boolean/object/function
  • True/False should this be wrapped in a promise (defaults to false)

Examples of all the permission types are below:

//Allways allow requests to /
ExpressPermissions.add(app, '/', true)
//Allow access to /admin if response.local.current_user.admin == 1
ExpressPermissions.add(app, '/admin', {current_user: {admin: 1}})
//Allow access to /admin/reboot if the supplied function returns true
ExpressPermissions.add(app, '/admin/reboot', function (req, res){
  return (req.ip == '127.0.0.1')
})

//Allow access if this promise resolves true
ExpressPermissions.add(app, '/project/:id/edit', function (req, res, resolve, reject){
  MyDatabase.query("SELECT * FROM `projects` WHERE `id` = '" + req.params.id + "' LIMIT 1").then(function(project){
      resolve(project.editable)
  })
}, true)

Permission checks travel upwards, in this example a request for /admin/index would see no permission itself and then try /admin. Due to this it is highly reccomended that you define something for / otherwise you may end up with routes that have no permissions.

Handling Error 403s

By default express-permissions ends the response preventing it from traveling any further this leaves the user with an empty reply which isn't really any good.

If you define app.permissionDenied it will be called when an error 403 is triggered instead of ending the response.

app.permissionDenied needs to take 2 arguments req and res and should work like any other express method.