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

to-locals

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

to-locals

transform callback-functions into connect middlewares, dumping their content to res.locals

  • 0.1.2
  • latest
  • npm
  • Socket score

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

to-locals: res.locals-middleware

Transform callback-functions into connect middlewares, dumping their content to res.locals.

How

npm i to-locals

var toLocals = require('to-locals');

// toLocals([context], function, [arguments], key)

toLocals(getUsers, 'users');
toLocals(getUserById, [ 'req.params.id' ], 'user');
toLocals(users, users.find, 'user');
toLocals(users, 'find', 'users');
toLocals(users, 'findById', [ 'req.params.id' ], 'user');

What

Most node function are something like this:

var getUser = function(cb) {
    cb(null, 'user');
};

Writing your site with express, you usually call these functions and just put their values in res.locals:

app.get('/user', function(req, res) {
    getUser(function(err, user) {
        res.render('index', {
            user: user
        });
    });
});

With to-locals, it's a bit simpler:

app.get('/', toLocals(getUser, 'user'), function(req, res) {
    res.render('index');
});

It's perfect for mongoose:

var users = toLocals(mongoose.model('users'), 'find', 'users');
app.get('users', users, [...]);

For more complicate cases you can to-locals around an anonymous function:

var project = toLocals(function (cb) {
    mongoose.model('projects').findById(cb.req.query.id, cb);
}, 'project');

Notice how req (and res) was attached to the callback!

Or use to-locals's sugar:

var project = toLocals(mongoose.model('projects'), 'findById', [ 'req.query.id' ], 'project');

Tests

Mocha with some npm test.

Licence

MIT

Keywords

FAQs

Package last updated on 10 Feb 2014

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