【发布时间】:2026-01-29 12:40:01
【问题描述】:
我正在为一个项目使用 Hapi.js,当我调用我的路由时,我传递给我的处理程序的配置变量将显示为 undefined。我做错了什么?
server.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();
routes.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;
}
controllers/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;
【问题讨论】:
标签: javascript node.js hapijs