Git Product home page Git Product logo

express-expose's Introduction

express-expose

NPM Version NPM Downloads Build Status Test Coverage

Expose helpers and local variables to the client-side.

Install

npm install -S express-expose

Usage

[email protected]:

var express = require('express');
var expose = require('express-expose');
app = expose(app);
app.expose(...);

[email protected] and [email protected]:

var express = require('express');
var expose = require('express-expose');
app.expose(...);

Versions

Examples

Exposing Objects

A common use-case for exposing objects to the client-side would be exposing some properties, perhaps the express configuration. The call to app.expose(obj) below defaults to exposing the properties to app.*, so for example app.views, app.title, etc.

app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('title', 'Example');
app.set('default language', 'en');

app.expose(app.settings);

Another use-case would be exposing helper methods, perhaps the same ones as you are currently exposing to templates. Below we expose the math object as utilities to our templates, as well as the client-side. Within a template we would call add(1,2), and on the CS we would call utils.add(1,2), since we have passed the namespace "utils".

var math = { add: function(a,b){ return a + b; } };
app.expose(math, 'utils').helpers(math);

Sometimes you might want to output to a different area, so for this we can pass an additional param "languages" which tells express which buffer to write to, which ends up providing us with the local variable "languages" in our template, where the default is "javascript". The "app" string here is the namespace.

app.expose({ en: 'English', fr: 'French' }, 'app', 'languages');

You'll then want to output the default buffer (or others) to your template, in Jade this would look something like:

script!= javascript

And in EJS:

<script><%- javascript %></script>

Raw JavaScript

It is also possible to expose "raw" javascript strings.

app.expose('var some = "variable";');

Optionally passing the destination buffer, providing us with the "head" local variable, instead of the default of "javascript".

app.expose('var some = "variable";', 'head');

Exposing Functions

Exposing a named function is easy too, simply pass it in with an optional buffer name for placement within a template much like above.

app.expose(function someFunction(){
  return 'yay';
}, 'foot');

Self-Calling Functions

Another alternative is passing an anonymous function, which executes itself, creating a "wrapper" function.

app.expose(function(){
  function notify() {
    alert('this will execute right away :D');
  }
  notify();
});

Request-Level Exposure

Finally we can apply all of the above at the request-level as well, below we expose "app.current.user" as { name: 'tj' }, for the specific request only.

app.get('/', function(req, res){
  var user = { name: 'tj' };
  res.expose(user, 'app.current.user');
  res.render('index', { layout: false });
});

License

MIT

express-expose's People

Contributors

arlolra avatar forbeslindesay avatar isstaif avatar jonpacker avatar niftylettuce avatar pgherveou avatar tj avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.