Loopbackjs 环回更改远程方法中的内容类型

Loopbackjs 环回更改远程方法中的内容类型,loopbackjs,strongloop,Loopbackjs,Strongloop,我需要找到一种方法来更改从自定义远程方法发送的响应的内容类型。默认情况下,它似乎是application/json 我有一个远程方法可以返回图像,因此我需要以某种方式更改内容类型。注册,然后在express上设置头,如resfrom context object。在结束时,调用下一个函数(如果已定义)继续执行 Model.afterRemote('fetch', function(ctx, instance, next) { ctx.res.header('Content-Type', '

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

我有一个远程方法可以返回图像,因此我需要以某种方式更改内容类型。

注册,然后在express上设置头,如
res
from context object。在结束时,调用下一个函数(如果已定义)继续执行

Model.afterRemote('fetch', function(ctx, instance, next) {
   ctx.res.header('Content-Type', 'image/png');
   next && next();
});
从上下文对象中注册并设置express上的头,如
res
。在结束时,调用下一个函数(如果已定义)继续执行

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

您可以将express的
响应
对象添加到方法签名中

Model.remoteMethod(
    'yourMethod',
    {
        accepts: [
            { arg: 'id', type: 'number', required: true },
            {arg: 'res', type: 'object', 'http': {source: 'res'}}
        ],
        http: {path: '/method/:id', verb: 'get'}
    }
);
然后使用此对象设置
内容类型

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

您可以将express的
响应
对象添加到方法签名中

Model.remoteMethod(
    'yourMethod',
    {
        accepts: [
            { arg: 'id', type: 'number', required: true },
            {arg: 'res', type: 'object', 'http': {source: 'res'}}
        ],
        http: {path: '/method/:id', verb: 'get'}
    }
);
然后使用此对象设置
内容类型

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