-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
context
example to a standalone app
- Loading branch information
Miroslav Bajtoš
committed
Nov 5, 2014
1 parent
885f4e0
commit 7a1a3b8
Showing
3 changed files
with
30 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |