Skip to content

Instantly share code, notes, and snippets.

@tzechienchu
Last active October 1, 2016 03:23
Show Gist options
  • Save tzechienchu/529e39ff637afab2de8765c5e05d6d9e to your computer and use it in GitHub Desktop.
Save tzechienchu/529e39ff637afab2de8765c5e05d6d9e to your computer and use it in GitHub Desktop.
Loopback Access IP
//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'))
@tzechienchu
Copy link
Author

next is not a function bug.

@tzechienchu
Copy link
Author

Add get Header

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment