gpt4 book ai didi

javascript - hapi 服务器出现错误

转载 作者:行者123 更新时间:2023-12-03 11:44:45 29 4
gpt4 key购买 nike

我正在构建一个简单的日志应用程序,并尝试实现模块化以使代码更具可读性。然而,当我启动我的应用程序时遇到了一些麻烦。我有两个文件: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;

最佳答案

您尝试在定义 landingPagegetWorkoutsgetMusclegroupaddWorkout 变量之前使用它们。因此,要解决这个问题,只需更改顺序,首先定义这些变量,然后创建路由:

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}
];

关于javascript - hapi 服务器出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26107825/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com