Last active
October 1, 2016 03:23
-
-
Save tzechienchu/529e39ff637afab2de8765c5e05d6d9e to your computer and use it in GitHub Desktop.
Loopback Access IP
This file contains hidden or 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
//Following Code in boot | |
module.exports = function (app) { | |
// | |
//Loopback 3.0 don't support app.remotes().before | |
// | |
app.remotes().before('*.*', function(ctx,next1,next2) { | |
//console.log(ctx.req.headers) | |
//console.log('IP-1') | |
var context = LoopBackContext.getCurrentContext(); | |
context.set('remoteAddress',ctx.req.connection.remoteAddress); | |
context.set('headers',ctx.req.headers); | |
var next; | |
if (typeof next1 === "function") { | |
next = next1; | |
} | |
if (typeof next2 === "function") { | |
next = next2; | |
} | |
next(); | |
}); | |
app.remotes().before('*.prototype.*', function(ctx,next1,next2) { | |
//console.log('IP-2') | |
LoopBackContext.getCurrentContext().set('remoteAddress',ctx.req.connection.remoteAddress); | |
var next; | |
if (typeof next1 === "function") { | |
//console.log('CB1') | |
next = next1; | |
} | |
if (typeof next2 === "function") { | |
console.log(next1) | |
next = next2; | |
} | |
next(); | |
}); | |
}; | |
//Following Code in Models | |
// var LoopBackContext = require('loopback-context'); | |
// var ctx = LoopBackContext.getCurrentContext(); | |
// console.log(ctx.get('remoteAddress')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add get Header