环回更改远程方法中的Content-Type

时间:2014-12-22 20:53:29

标签: loopbackjs strongloop

我需要找到一种方法来更改我从自定义远程方法发送的响应的内容类型。默认情况下它似乎是application / json。

我有一个返回图像的远程方法,因此我需要以某种方式更改Content-Type。

2 个答案:

答案 0 :(得分:4)

注册remote hook,然后在上下文对象的类快捷res上设置标题。最后调用next函数(如果已定义)继续执行。

Model.afterRemote('fetch', function(ctx, instance, next) {
   ctx.res.header('Content-Type', 'image/png');
   next && next();
});

答案 1 :(得分:1)

您可以将express的response对象添加到方法签名

Model.remoteMethod(
    'yourMethod',
    {
        accepts: [
            { arg: 'id', type: 'number', required: true },
            {arg: 'res', type: 'object', 'http': {source: 'res'}}
        ],
        http: {path: '/method/:id', verb: 'get'}
    }
);

然后使用此对象设置content-type

Model. yourMethod = function(id, res, cb) {
    res.type("image/png");
    cb();
}
相关问题