File tree Expand file tree Collapse file tree 3 files changed +30
-5
lines changed Expand file tree Collapse file tree 3 files changed +30
-5
lines changed Original file line number Diff line number Diff line change @@ -18,10 +18,6 @@ CartItem.sum = function(cartId, callback) {
18
18
return prev + cur ;
19
19
} , 0 ) ;
20
20
21
- var ns = loopback . getCurrentContext ( ) ;
22
- if ( ns && ns . get ( 'http' ) ) {
23
- console . log ( 'Remote call via url: %s' , ns . get ( 'http' ) . req . url ) ;
24
- }
25
21
callback ( null , total ) ;
26
22
} ) ;
27
23
}
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ var memory = loopback.createDataSource({
5
5
connector : loopback . Memory
6
6
} ) ;
7
7
8
- server . use ( loopback . rest ( { context : { enableHttpContext : true } } ) ) ;
8
+ server . use ( loopback . rest ( ) ) ;
9
9
server . model ( CartItem ) ;
10
10
11
11
CartItem . attachTo ( memory ) ;
Original file line number Diff line number Diff line change
1
+ var loopback = require ( '../../' ) ;
2
+ var app = loopback ( ) ;
3
+
4
+ // Create a LoopBack context for all requests
5
+ app . use ( loopback . context ( ) ) ;
6
+
7
+ // Store a request property in the context
8
+ app . use ( function saveHostToContext ( req , res , next ) {
9
+ var ns = loopback . getCurrentContext ( ) ;
10
+ ns . set ( 'host' , req . host ) ;
11
+ next ( ) ;
12
+ } ) ;
13
+
14
+ app . use ( loopback . rest ( ) ) ;
15
+
16
+ var Color = loopback . createModel ( 'color' , { 'name' : String } ) ;
17
+ Color . beforeRemote ( '**' , function ( ctx , unused , next ) {
18
+ // Inside LoopBack code, you can read the property from the context
19
+ var ns = loopback . getCurrentContext ( ) ;
20
+ console . log ( 'Request to host' , ns && ns . get ( 'host' ) ) ;
21
+ next ( ) ;
22
+ } ) ;
23
+
24
+ app . dataSource ( 'db' , { connector : 'memory' } ) ;
25
+ app . model ( Color , { dataSource : 'db' } ) ;
26
+
27
+ app . listen ( 3000 , function ( ) {
28
+ console . log ( 'A list of colors is available at http://localhost:3000/colors' ) ;
29
+ } ) ;
You can’t perform that action at this time.
0 commit comments