首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在feathersjs websocket api中响应纯文本?

如何在feathersjs websocket api中响应纯文本?
EN

Stack Overflow用户
提问于 2017-06-29 15:21:03
回答 1查看 323关注 0票数 0

我定义了一个羽化服务api,如下:

代码语言:javascript
运行
复制
class Monitor {

  find(_) {
    const metrics = prom.register.metrics();
    log.info(metrics);
    return new Promise((resolve) => {
      resolve({text: metrics});
    });
  }
}

function restFormatter(req, res) {
  res.format({
    'text/plain': function() {
      log('xxxx:', res);
      res.end(`The Message is: "${res.data}"`);
    }
  });
}

module.exports = function () {
  const app = this;

  // Initialize our service with any options it requires
  const service = new Monitor();
  app.configure(rest(restFormatter)).use('/metrics', service);

  // Get our initialize service to that we can bind hooks
  const monitorService = app.service('/metrics');

  // Set up our before hooks
  monitorService.before(hooks.before);

  // Set up our after hooks
  monitorService.after(hooks.after);
  return service;
};

module.exports.Monitor = Monitor;

在浏览器调用此接口时,得到如下响应:

代码语言:javascript
运行
复制
"# HELP nodejs_gc_runs_total Count of total garbage collections.\n# TYPE nodejs_gc_runs_total counter\n\n# HELP nodejs_gc_pause_seconds_total Time spent in GC Pause in seconds.\n# TYPE nodejs_gc_pause_seconds_total counter\n\n# HELP nodejs_gc_reclaimed_bytes_total Total number of bytes reclaimed by GC.\n# TYPE nodejs_gc_reclaimed_bytes_total counter\n"

从上面的输出中,您可以看到feathersjs不会以纯文本格式返回数据。它会将我响应文本转换为字符串。以下是浏览器中显示的express服务的输出:

代码语言:javascript
运行
复制
# HELP nodejs_gc_runs_total Count of total garbage collections.
# TYPE nodejs_gc_runs_total counter

# HELP nodejs_gc_pause_seconds_total Time spent in GC Pause in seconds.
# TYPE nodejs_gc_pause_seconds_total counter

# HELP nodejs_gc_reclaimed_bytes_total Total number of bytes reclaimed by GC.
# TYPE nodejs_gc_reclaimed_bytes_total counter

# HELP newConnection The number of requests served
# TYPE newConnection counter

这个输出才是我真正想要的。我怎样才能让他们的feathersjs服务返回高于输出?

以下是我的feathersjs配置部分:

代码语言:javascript
运行
复制
app
  .use(compress())
  .options('*', cors())
  .use(cors())
  .use('/', serveStatic(app.get('public')))
  .use(bodyParser.json())
  .use(bodyParser.urlencoded({extended: true}))
  .configure(hooks())
  .configure(rest())
  .configure(
    swagger({
      docsPath: '/docs',
      uiIndex: path.join(__dirname, '../public/docs.html'),
      info: {
        title: process.env.npm_package_fullName,
        description: process.env.npm_package_description
      }
    })
  )
  .configure(
    primus(
      {
        transformer: 'websockets',
        timeout: false
      },
      (primus) => {
        primus.library();
        primus.save(path.join(__dirname, '../public/dist/primus.js'));
      }
    )
  )
  .configure(services)
  .configure(middleware);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-01 04:21:11

您配置了两次feathers-rest,这就是为什么您仍然会得到旧的输出。从服务文件中删除app.configure(rest(restFormatter)),然后在主文件中将.configure(rest())更改为.configure(rest(restFormatter))以使用格式化程序应用于所有服务,或者注册一个仅对该服务执行格式化的custom middleware for the service

代码语言:javascript
运行
复制
app.use('/metrics', service, function(req, res) {
  res.format({
    'text/plain': function() {
      log('xxxx:', res);
      res.end(`The Message is: "${res.data}"`);
    }
  });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44818726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档