Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-sticker

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-sticker

Group your express routes to access them globally and save the routes path in a configuration file

  • 0.1.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

express-sticker

Group your express routes to access them globally and save the routes path in a configuration file

Getting Started

Group your express routes to access them globally from all your application and save the routes path in a configuration file. You can add dependencies in order to structure your code correctly. Check the usage examples to see how it's working.

npm install express-sticker

Usage examples

Initialization of the sticker
var express = require("express");
var app = express();
var stick = require("sticker").use(app);

Configure the express application with the use method.

Creation of the sticks
  • Every sticks must call next(error, params) at the end of its execution.
  • The params of all the dependencies are merged in one object.
  • If there is an error in the dependencies, the execution of the dependencies is stopped and the main stick is called with the error in req.err.
  • It's possible to override a stick who is using res.end(), its res.end() will be ineffective but yours will work.
  • If you want to force the end, the method res.forceEnd() is available, usefull for some stuff like the checkLogin example. In this case do not call the next callback.
var stick = require("sticker");

stick("checkLogin", function(next, params, req, res) {
    if (!req.user) {
        res.redirect("/login");
        res.forceEnd();
    } else {
        next(false);
    }
});

stick("checkAvailability", function(next) {
    next(null);
});

var fetchUserData = stick("fetchUserData", ["checkAvailability"], function(next, params) {
    next(null, {user: "jack"});
});

stick("displayUser", ["checkLogin", fetchUserData], function(next, params, req, res) {
    if (req.error)
        res.end("error");
    else
        res.end(params.user);
    next(true, params);
});

stick("overrideDisplayUser", ["displayUser"], function(next, params ,req, res) {
    res.end("user: " + params.user);
    next(null, params);
});

// Override a stick
stick("getMoney", function(next, params) {
    return next(null, {money: 500});
});

var getMoney = stick("getMoney", function(next, params) {
    params.money += 200;
    return next(null, params);
});

getMoney(function(err, params) {
    console.log("700 = ", params.money);
});
Get a stick and execute it
var myStick = stick("fetchUserData");
myStick(function(err, params) {
    console.log("stick executed");
});
// Or
stick.execute(myStick, function(err, params) {
    console.log("stick executed");
});
// Or
stick.execute("fetchUserData", {type: "poney"}, function(err, params) {
    console.log("stick executed, user = ", params.user);
});
// Remove it
stick.remove("fetchUserData");
Initialization of the routes file
  • Possibility to add multiple files
  • Possibility to pass params to the routes file.
  • You must add the routes after the sticks are loaded.
var params = {prod: true};
var err = stick.addRoutes(__dirname + "/routes.js", params);
Routes file example:
  • action: The app.VERB() method provided by routing functionality in Express
  • path: path to call the route
  • stick: stick that will be executed
  • enabled: to disable/enable the route, default is true
module.exports = function(params) {
    return [{action: "get", path: "/user", stick: "displayUser"},
            {action: "get", path: "/wall", stick: "displayWall"},
            {action: "get", path: "/admin", stick: "displayAdmin", enabled: params.prod}];
}

License

(The MIT License)

Copyright (c) 2013 Maxime Mezrahi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 26 Dec 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc