我正在将 Hapi.js 用于一个项目,当我调用我的路由时,我传递给我的处理程序的配置变量出现未定义。我究竟做错了什么?
服务器.js
var Hapi = require('hapi');
var server = new Hapi.Server('0.0.0.0', 8080);
// passing this all the way to the handler
var config = {'number': 1};
var routes = require('./routes')(config);
server.route(routes);
server.start();
路由.js
var Home = require('../controllers/home');
module.exports = function(config) {
var home = new Home(config);
var routes = [{
method: 'GET',
path: '/',
handler: home.index
}];
return routes;
}
控制器/home.js
var Home = function(config) {
this.config = config;
}
Home.prototype.index = function(request, reply) {
// localhost:8080
// I expected this to output {'number':1} but it shows undefined
console.log(this.config);
reply();
}
module.exports = Home;