expressjs / express-expose Goto Github PK
View Code? Open in Web Editor NEWExpose raw js, objects, and functions to the client-side (awesome for sharing utils, settings, current user data etc)
License: MIT License
Expose raw js, objects, and functions to the client-side (awesome for sharing utils, settings, current user data etc)
License: MIT License
@jonathanong can you please add this repo at https://coveralls.io/repos/new? I don't seem to have permission to atm, thanks!
Couldn't see an existing issue for this, apart from the pull request with the test.
app.get('/', function(req, res) {
res.expose('var something = true;');
res.render('index');
});
The JS string printed in the template omits the stuff added at the request level. Using express 3 & expose 0.3.2.
I am getting the following error:
500 ReferenceError: /private/tmp/express-expose/examples/views/index.jade:4 2| head 3| title= settings.title > 4| script!= javascript 5| body Hello javascript is not defined
app.js
/**
* Module dependencies.
*/
var express = require('express')
, http = require('http')
, path = require('path')
, expose = require('../../express-expose')
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.expose(app.settings);
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
index.jade
html
head
title= settings.title
script!= javascript
body Hello
when app.exposed(name)
and res.exposed(name)
return empty strings the helper should also return an empty string in line 69-75:
helpers[name] = function(req, res){
var appjs = app.exposed(name),
resjs = res.exposed(name);
if (appjs || resjs) {
var js = '// app: \n' + appjs;
js += '// res: \n' + resjs;
return js;
} else return "";
};
ref: expressjs/discussions#134
@tj , @niftylettuce - you have been identified as the last / most active committer in this repo which is inactive for a while. This ping is to check with you to determine:
thanks!
Is there any reason that you can't? I was just making an app and I need to send data to the client-side
javascript var isn' t exposed anymore using expose with express 3 ?
creating an app with express command line, adding expose and this line to the template this failed for me
script!= javascript
I'm trying to directly expose a mongoose object within the callback of the mongoose's find method. When calling res.expose(mongooseObject, 'mongooseObjectName') this results in a the following error:
RangeError: Maximum call stack size exceeded
I don't know if this is an error caused by mongoose or by expose. Does anybody know what's happening here and how i could fix it?
first off, neat project.
If a project had a large of Javascript that was exposed to the client side (e.g. several modules), would it make sense to have these as "script" includes rather than writing them into the page ?
J
When exposing mongoose model I have to explicitly call
model._id.toString() to have the id correctly encoded in the client page
otherwise it get encoded lilke that
Do you have any any better solution to use expose with this kind of object ?
myModel["_id"] = {"id":"N�� \u001fB> 5\u0000\u0000\u0005"};
something like:
res.expose(obj).as('app.settings')
res.expose(obj).as('app.settings').in('head')
Hi, looks like the node version in package.json needs to be modified to be compatible with node v0.6.x
Tests are passing on 0.2.x but they are all failing under 0.3.x
Is everything ok on your computer ?
Here is my environment info
$ node -v
v0.6.19
$ npm ls
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│ └── [email protected]
└── [email protected]
Hi TJ
express-expose define the following function to create a string representation of objects
function string(obj) {
if ('function' == typeof obj) {
return obj.toString();
} else if (obj instanceof Date) {
return 'new Date("' + obj + '")';
} else if (Array.isArray(obj)) {
return '[' + obj.map(string).join(', ') + ']';
} else if ('[object Object]' == Object.prototype.toString.call(obj)) {
return '{' + Object.keys(obj).map(function(key){
return '"' + key + '":' + string(obj[key]);
}).join(', ') + '}';
} else {
return JSON.stringify(obj);
}
}
This works well except when used with mongo ObjectId object, where I end up with this serialisation
window.someId = window.someId || {};
someId["_bsontype"] = "ObjectID";
someId["id"] = "OÏ�\u001a¾�Å°U\u0000\u00000";
for my use case, I could replace the Object.prototype.toString statement with obj.toString (toString return string representation on ObjectId)
...
// else if ('[object Object]' == Object.prototype.toString.call(obj)) {
else if ('[object Object]' == obj.toString.call(obj)) {
Could the function be exposed somehow so I can inject my customisations ?
Or do you see a better solution for this kind of issues ?
My code for requiring expose looks like this:
------------------BEGIN CODE---------------------
var express = require('express')
, expose = require('express-expose')
, program = require('commander')
, cookie = require('cookie')
, routes = require('./routes')
, api = require('./routes/api.js')
, passport = require('passport')
, BasicStrategy = require('passport-http').BasicStrategy
, config = require('config')
;
program
.version('0.0.1')
.option('-t, --test', 'Run as a test')
.parse(process.argv);
var app = module.exports = express.createServer();
------------------END CODE---------------------
The specific line in express-expose.js throwing the error is line 44.
Would be nice if multiple calls to expose used the last in wins approach. It seems that the first call exposing a function or data is persisted.
This module creates content for an inline <script>
tag. However, if anything in the content of that script tag includes </script>
, HTML5's parsing rules will prematurely end the tag there, allowing for XSS injection.
This module should modify any strings in the returned JSON (for keys or values) that include </script>
to be written as </"+"script>
.
Not sure if it's considered an issue but when require('express-expose')
is after var app = require('express')()
, there is an error:
TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'expose'
Code example that fails:
express = require('express')
app = express()
require('express-expose')
app.expose
Code example that works:
express = require("express")
require('express-expose')
app = express()
app.expose
It might strike as an odd use case but it happened while porting an app to express3.
Hi guys
Just a quick issue: from a mint express project, this doesn't work with just:
expose = require('express-expose')
app.expose()
I also had to add:
"script!= javascript" in my jade template
Thanks for this, works beautifully!
@tj @jonathanong can we add me as an NPM owner to the express-expose
package in NPM? I need to version bump. Thanks guys!
Cannot read property 'prototype' of undefined
at Object. (/**********/node_modules/express-expose/lib/express-expose.js:51:11)
var secure_keys,
express = require("express"),
expose = require("express-expose"), // When I remove this line my app can start:
app = express.createServer(secure_keys);
node -v:
6.0.13
and npm:
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
│ └── [email protected]
└─┬ [email protected]
└── [email protected]
Heya, is it possible to make express-expose work with node 0.5?
Thanks! :D
$ npm -d install express-expose
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info addNamed [ 'express-expose', '' ]
npm ERR! Error: No compatible version found: express-expose
npm ERR! No valid targets found.
npm ERR! Perhaps not compatible with your version of node?
npm ERR! at installTargetsError (/opt/local/lib/node_modules/npm/lib/cache.js:424:10)
npm ERR! at /opt/local/lib/node_modules/npm/lib/cache.js:406:17
npm ERR! at saved (/opt/local/lib/node_modules/npm/lib/utils/npm-registry-client/get.js:136:7)
npm ERR! at Object.cb [as oncomplete] (/opt/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:36:9)
npm ERR! Report this *entire* log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <[email protected]>
npm ERR!
npm ERR! System Darwin 11.2.0
npm ERR! command "node" "/opt/local/bin/npm" "-d" "install" "express-expose"
npm ERR! cwd /Volumes/Fuitad/Documents/Dev/
npm ERR! node -v v0.5.10
npm ERR! npm -v 1.0.106
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Volumes/Fuitad/Documents/Dev/npm-debug.log
npm not ok
For some odd reason, IE8 isn't happy with this:
https://github.com/visionmedia/express-expose/blob/master/lib/express-expose.js#L218
var test = test || {};
causes a problem. I'm guessing it has something to do with hoisting replacing the global variable.
In any case, swapping this,
return (i ? '' : 'var ') + part + ' = ' + part + ' || {};';
with this,
return (i ? '' : 'window.') + part + ' = window.' + part + ' || {};';
gets around the problem. Not sure you want to do that, just thought I'd point it out.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.