Skip to content

Commit

Permalink
Move context example to a standalone app
Browse files Browse the repository at this point in the history
  • Loading branch information
Miroslav Bajtoš committed Nov 5, 2014
1 parent 885f4e0 commit 7a1a3b8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 0 additions & 4 deletions example/client-server/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ CartItem.sum = function(cartId, callback) {
return prev + cur;
}, 0);

var ns = loopback.getCurrentContext();
if (ns && ns.get('http')) {
console.log('Remote call via url: %s', ns.get('http').req.url);
}
callback(null, total);
});
}
Expand Down
2 changes: 1 addition & 1 deletion example/client-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var memory = loopback.createDataSource({
connector: loopback.Memory
});

server.use(loopback.rest({context: {enableHttpContext: true}}));
server.use(loopback.rest());
server.model(CartItem);

CartItem.attachTo(memory);
Expand Down
29 changes: 29 additions & 0 deletions example/context/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var loopback = require('../../');
var app = loopback();

// Create a LoopBack context for all requests
app.use(loopback.context());

// Store a request property in the context
app.use(function saveHostToContext(req, res, next) {
var ns = loopback.getCurrentContext();
ns.set('host', req.host);
next();
});

app.use(loopback.rest());

var Color = loopback.createModel('color', { 'name': String });
Color.beforeRemote('**', function (ctx, unused, next) {
// Inside LoopBack code, you can read the property from the context
var ns = loopback.getCurrentContext();
console.log('Request to host', ns && ns.get('host'));
next();
});

app.dataSource('db', { connector: 'memory' });
app.model(Color, { dataSource: 'db' });

app.listen(3000, function() {
console.log('A list of colors is available at http://localhost:3000/colors');
});

0 comments on commit 7a1a3b8

Please sign in to comment.