물론을 사용하여 REQ 객체를 표현하기 위해 액세스 할 수 있습니다, 당신은 ctx.args
속성을 수정하는 beforeRemote
후크를 사용할 수 있습니다. 이 속성은 원격 메서드 (즉, 사용자 지정 또는 기본 제공)의 입력입니다. 이렇게하면이 속성 내에 요청의 일부를 복사 할 수 있으며 내장 메서드 findOne
을 사용하여
예제 1의 예제 1을 복사 할 수 있습니다.
MyModel.beforeRemote('findOne', function (ctx, model, next) {
ctx.args.filter.extrafield = ctx.req.headers['some-header'];
next();
});
다음 override 당신이
MyModel.on('dataSourceAttached', function(obj){
var findOne = MyModel.findOne;
MyModel.findOne = function(filter, cb) {
console.log(filter.extrafield); // Print what was in the header
return findOne.apply(this, arguments);
};
});
을 그리고 마지막으로 원격 사용자 정의 printToken
와
curl -H "some-header: 'hello, world!'" localhost:3000/api/MyModel/findOne
컬
예 2와 메소드를 호출 할 무엇 때문에 findOne 방법, 추가 이해를 돕기 위해
MyModel.beforeRemote('printToken', function (ctx, model, next) {
ctx.args.token = ctx.req.headers['some-header'];
next();
});
MyModel.printToken = function(token, cb) {
console.log(token);
cb();
}
MyModel.remoteMethod(
'printToken',
{
accepts: {arg: 'token', type: 'string', optional: true}
}
);
그리고 곱슬 원격 호출하고, 예상되는 헤더를 통과
curl -H "some-header: 'hello, world!'" localhost:3000/api/MyModel/printToken
편집 :는 는 사용자 정의 작동 간단한 해결책이 원격
원격 방법을 정의 , 루프백에게 입력 요청으로 직접 리모콘에 http 요청 요소를 전달할 수 있습니다.
MyModel.remoteMethod(
'printToken',
{
accepts: [
{arg: 'req', type: 'object', 'http': {source: 'req'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}}
]
}
);
이렇게하면 리모컨이 req 및 res 객체에 액세스 할 수 있습니다. 이것은 문서화되어 있습니다. here
제가 알기로, 모델의 빌트인 CRUD 메소드 (정확하게는 무시 된 버전)에서 액세스하고 싶습니다. 나는 원격 훅 (remote hooks), 심지어는 원격 메소드 (remote methods)에서 문맥에 접근하는 데 문제가 없다. – Aleks