• Stars
    star
    299
  • Rank 139,269 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Expose raw js, objects, and functions to the client-side (awesome for sharing utils, settings, current user data etc)

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

More Repositories

1

express

Fast, unopinionated, minimalist web framework for node.
JavaScript
63,539
star
2

multer

Node.js middleware for handling `multipart/form-data`.
JavaScript
11,285
star
3

morgan

HTTP request logger middleware for node.js
JavaScript
7,790
star
4

session

Simple session middleware for Express
JavaScript
6,163
star
5

cors

Node.js CORS middleware
JavaScript
5,961
star
6

body-parser

Node.js body parsing middleware
JavaScript
5,376
star
7

expressjs.com

HTML
5,138
star
8

compression

Node.js compression middleware
JavaScript
2,722
star
9

csurf

CSRF token middleware
2,299
star
10

cookie-parser

Parse HTTP request cookies
JavaScript
1,907
star
11

generator

Express' application generator
JavaScript
1,803
star
12

serve-static

Serve static files
JavaScript
1,368
star
13

cookie-session

Simple cookie-based session middleware
JavaScript
1,104
star
14

vhost

virtual domain hosting
JavaScript
758
star
15

serve-favicon

favicon serving middleware
JavaScript
620
star
16

method-override

Override HTTP verbs.
JavaScript
614
star
17

response-time

Response time header for node.js
JavaScript
458
star
18

serve-index

Serve directory listings
JavaScript
435
star
19

errorhandler

Development-only error handler middleware
JavaScript
423
star
20

express-paginate

Paginate middleware
JavaScript
419
star
21

connect-multiparty

connect middleware for multiparty
JavaScript
347
star
22

express-namespace

Adds namespaced routing capabilities to Express
JavaScript
345
star
23

timeout

Request timeout middleware for Connect/Express
JavaScript
312
star
24

basic-auth-connect

Basic auth middleware for node and connect
JavaScript
129
star
25

domain-middleware

`uncaughtException` middleware for connect, base on `domain` module.
JavaScript
101
star
26

api-error-handler

Express error handlers for JSON APIs
JavaScript
100
star
27

flash

JavaScript
92
star
28

restful-router

Simple RESTful url router.
JavaScript
86
star
29

urlrouter

http url router, `connect` missing router middleware
JavaScript
59
star
30

discussions

Public discussions for the Express.js organization
55
star
31

vhostess

virtual host sub-domain mapping
JavaScript
24
star
32

connect-markdown

Auto convert markdown to html for connect.
JavaScript
20
star
33

expressjs.github.io

16
star
34

connect-rid

connect request id middleware
JavaScript
11
star
35

routification

DEPRECATED
JavaScript
10
star
36

mime-extended

DEPRECATED - Please use mime-types instead.
JavaScript
7
star
37

statusboard

A project status board for the Express community
6
star
38

.github

5
star
39

set-type

DEPRECATED - Please use mime-types instead.
JavaScript
5
star
40

security-wg

Express.js Security Working Group
4
star
41

Admin

Admin repository for the Express Organization, including pillarjs and jshttp
2
star