我有2条路线test_01和test_02。
当请求到达test_02路由时,如何访问路由test_01并获取其响应数据?

我不是在谈论从test_02到test_01的转发路由

server.route({
    method: 'GET',
    path: '/data/test_01',
    handler: function (request, reply) {
        reply('test_01')
    }
})

server.route({
    method: 'GET',
    path: '/data/test_02',
    handler: function (request, reply) {
        // Get data from route '/data/test_01'
        var test_01_data = ''
        reply(test_01_data)
    }
})


控制者

Index = function () {}

Index.prototype = {

    test_01 : function (req, reply, data) {
        reply('test_01')
    },

    test_02 : function (req, reply, data) {
        reply('test_02')
    }
}

module.exports = Index

最佳答案

有一个单独的控制器函数要从处理程序中调用

var controllerfn = function(req,reply){
     //do stuff
  return 'test_01';
};


根据需要调用

server.route({
  method: 'GET',
  path: '/data/test_01',
  handler: function (request, reply) {
             reply(controllerfn(request,reply);
  }
})

server.route({
  method: 'GET',
  path: '/data/test_02',
  handler: function (request, reply) {
    // Get data from route '/data/test_01'
    var test_01_data = controllerfn(request,reply);
    reply(test_01_data)
  }
})

10-04 22:17
查看更多