我正在构建一个简单的日志应用程序,并尝试实现模块化以使代码更具可读性。但是,当我启动我的应用程序时,我遇到了一些麻烦。我有两个文件index.js
,这是我的服务器配置,routes.js
我有我的路由逻辑。
我编写路由的方式与the hapi api中的api文档一致。
我希望有人能帮助我理解为什么我会收到以下错误:
/Users/mario/projects/loGym/node_modules/hapi/node_modules/hoek/lib/index.js:425
throw new Error(msgs.join(' ') || 'Unknown error');
^
Error: Missing or undefined handler: /
at Object.exports.assert (/Users/mario/projects/loGym/node_modules/hapi/node_modules/hoek/lib/index.js:425:11)
at new module.exports.internals.Route (/Users/mario/projects/loGym/node_modules/hapi/lib/route.js:36:10)
at /Users/mario/projects/loGym/node_modules/hapi/lib/router.js:110:25
at Array.forEach (native)
at /Users/mario/projects/loGym/node_modules/hapi/lib/router.js:107:17
at Array.forEach (native)
at internals.Router.add (/Users/mario/projects/loGym/node_modules/hapi/lib/router.js:104:13)
at internals.Server._route (/Users/mario/projects/loGym/node_modules/hapi/lib/server.js:471:18)
at internals.Server.route (/Users/mario/projects/loGym/node_modules/hapi/lib/server.js:465:10)
at Object.<anonymous> (/Users/mario/projects/loGym/index.js:10:8)
我没有正确导出模块吗?
以下是routes.js
的代码:
var path = require('path');
var _ = require('underscore');
var couchbase = require('couchbase');
//Connect to database.
var db = db || new couchbase.Connection({host: 'localhost:8091', bucket: 'default'}, function(err) {
if (err) {
console.log('Connection Error', err);
} else {
console.log('Connected!');
}
});
console.log(db);
module.exports = [
{method: 'GET', path: '/static/{param*}', config: { handler: { directory: { path: 'static'}}}},
{method: 'GET', path:'/', config: landingPage},
{method: 'GET', path:'/workouts', config: getWorkouts},
{method: 'GET', path:'/workouts/musclegroup', config: getMusclegroup},
{method: 'POST', path:'/addworkout', config: addWorkout}
];
var landingPage = {
handler: function(req, reply) {
reply.file('index.html');
}
};
var getWorkouts = {
handler: function (req, reply) {
// set options for databse query
var q ={
descending: true,
stale: false
};
// show multiple exercises - db.view(designDocument, viewName, options)
db.view('workout', 'exercise', q).query(function(err, values){
// use pluck method from underscore to retrieve data
var keys = _.pluck(values, 'id');
console.log("Keys: " + keys);
//fetch multiple documents based on the 'keys' object
db.getMulti(keys, null, function(err, results){
console.log('Results: ' + results);
var workouts = [];
for (var prop in results) {
workouts.push(results[prop].value);
}
reply(workouts);
});
});
}
};
var getMusclegroup = {
handler: function (req, reply) {
var q = {
descending: true,
stale: false
};
db.view('workout', 'exercise', q).query(function(err, values){
var keys = _.pluck(values, 'id');
db.getMulti(keys, null, function(err, results){
var muscleGroups = [];
for (var prop in results) {
console.log(typeof results);
console.log(results[prop].value.workout);
muscleGroups.push(results[prop].value.workout);
}
reply(muscleGroups[0]);
});
});
}
};
var addWorkout = {
handler: function(req, reply){
var d = new Date();
var cd = d.getDate() + "-" + (d.getMonth()+1) + "-" + d.getFullYear();
// sets schema for workout
var payload = {
"personId": "personId", //to later be replaced with actual username
"date": cd,
"workout": [
{
"exercise": req.query.exercise,
"musclegoup": req.query.musclegroup,
"sets": [
{
"reps": req.query.reps,
"kg": req.query.kg
}
]
}
]
};
// defines unique key for data
var key = payload.personId + payload.date;
console.log(key);
// adds payload to database
db.add(key, payload, function(error, results){
if (error) {
console.log(error);
reply(error + "\n");
}
console.log(results);
reply(payload);
});
}
};
这是我index.js
的代码:
var Hapi = require('hapi');
var path = require('path');
var Joi = require('joi');
var rs = require('./lib/modules/routes.js');
var config= { };
var server = Hapi.createServer(process.env.PORT || 8080, config);
server.route(rs);
server.start(function(){
console.log("Server started: " + server.info.uri);
});
module.exports = server;
答案 0 :(得分:1)
您在定义之前尝试使用landingPage
,getWorkouts
,getMusclegroup
和addWorkout
变量。因此,要解决此问题,只需更改顺序,首先定义这些变量,然后创建路径:
var landingPage = {
...
};
var getWorkouts = {
...
};
var getMusclegroup = {
...
};
var addWorkout = {
...
};
module.exports = [
{method: 'GET', path: '/static/{param*}', config: { handler: { directory: { path: 'static'}}}},
{method: 'GET', path:'/', config: landingPage},
{method: 'GET', path:'/workouts', config: getWorkouts},
{method: 'GET', path:'/workouts/musclegroup', config: getMusclegroup},
{method: 'POST', path:'/addworkout', config: addWorkout}
];