0
我正在構建一個簡單的日誌應用程序,並試圖實現模塊化以使代碼更具可讀性。然而,當我啓動我的應用程序時,我遇到了一些麻煩。我有兩個文件,index.js
這是我的服務器配置,並有routes.js
我有我的路由邏輯。 我寫過我的路線的方式是根據the hapi api中的api文檔。在hapi服務器中獲取錯誤
我希望有人可以幫助我瞭解爲什麼我收到以下錯誤:
/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;
我不敢相信我沒有意識到這一點!這樣的初學者錯誤!非常感謝! – hyprstack 2014-09-29 20:34:39