2016-03-07 60 views
0

我試圖根據URL發送響應。希望檢查是否可以檢查網址並在onPreResponse中發送回覆。這是我想要做的事情。在Hapi.js的onPreResponse中切換JSON /查看

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(); 
}); 

如何分辨高致病性禽流感如果我正確理解你的問題使用視圖渲染

+0

爲什麼這必須在'onPreResonse'內?任何不使用標準路由功能的理由(即路徑爲'/ api/{path *}'的路徑)? –

+0

這是一個好主意。一個例子,將不勝感激。 –

+0

我需要看到更多的代碼才能明白你想要達到的目標。 –

回答

0

,爲什麼不能定義兩個路線?

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