在 JSON/View in onPreResponse in happy.js 之间切换

Switch between JSON / View in onPreResponse in Hapi.js

本文关键字:in js 之间 happy onPreResponse JSON View      更新时间:2023-09-26

>我正在尝试根据URL发送响应。想检查是否可以在 onPreResponse 中检查 url 并发送响应。这是我想做的事情。

server.ext('onPreResponse', function (request, reply) {
  if (request.path.indexOf('/api') > -1 ) {
     // Do nothing. Let the response go as JSON
  } else {
     // Send back html by populating json data into 
     // handlebar template
     reply.view('layout', request.response.source);  // Need replacement for this
  }
  reply.continue();
});

如何告诉 HAPI 使用视图进行渲染

如果我正确理解你的问题,为什么你不能定义两条路线?

[{
    method: 'GET',
    path:'/',
    handler: function (request, reply) {
        reply.view('layout', request.response.source);
    }
},
{
    method: ['GET','POST'],
    path:'/api',
    handler: function (request, reply) {
        reply(result);
    }
}]