Node Js.get()需要回调函数
Node Js .get() requires callback functions
这是我的server.js代码
var express = require('express');
feeds = require('./routes/whatshappeningfeed');
var http = require('http');
var pathname = require('path');
// Test services - to be removed
courses = require('./routes/courses');
auth = require('./routes/auth');
token = require('./routes/token');
var app = express();
app.configure(function () {
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
});
app.use(function (error, req, res, next) {
if (!error) {
next();
} else {
console.error(error.stack);
res.send(500);
}
});
app.get('/me/whatshappeningfeed',feeds.getfeeds);
app.get('/courses', courses.findAll);
app.get('/courses/:id', courses.findById);
app.get('/token', token.auth);
app.get('/auth', auth.auth);
app.get('/refresh', auth.refresh);
app.listen(80);
console.log('Listening on port 80...');
这是我的错误信息:
F:'NODE.JS'poc'node_modules'express'lib'router'index.js:291
throw new Error(msg);
^
Error: .get() requires callback functions but got a [object Undefined]
at F:'NODE.JS'poc'node_modules'express'lib'router'index.js:291:11
at Array.forEach (native)
at Router.route (F:'NODE.JS'poc'node_modules'express'lib'router'index.js:287:13)
at Router.(anonymous function) [as get] (F:'NODE.JS'poc'node_modules'express'lib'router'index.js:318:16)
at Function.app.(anonymous function) [as get] (F:'NODE.JS'poc'node_modules'express'lib'application.js:431:26)
at Object.<anonymous> (F:'NODE.JS'poc'server.js:44:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
当我运行nodeserver.js文件时,我得到了上面的错误。它工作得更早。我无法解决这个问题。请任何人帮帮我。
错误说明了一切,app.get()需要一个回调函数。因此,这意味着您的一个或多个路由缺少回调。
所有的路由都应该有一个函数(req,res),无论是像示例中那样明确,还是在另一个函数中(见注释):
app.get('/me/whatshappeningfeed', function(req,res){ //request, response
//then here you can define what your server should send as a response when queries for /me/whatshappeningfeed
res.send(feeds.getfeeds()); //this will send back to the browser the result of feeds.getfeeds()
});
此外,不确定是否是由于您所做的复制粘贴,但第一个分号require('express');
应该是逗号。
相关文章:
- JavaScript回调函数
- 如何在回调函数中执行流
- 回调函数中传递参数的困难(Google Map API Markers)
- 如何正确地将参数传递给RequireJS回调函数
- 回调函数在python代码中离线
- 从AJAX回调函数中分离数据
- 赋值后的回调函数
- 如何在javascript回调函数中返回多个变量
- 函数表达式,返回回调函数
- 等待回调函数执行
- JavaScript回调函数和Google Feed API
- 如何从ajax成功回调函数中读取javascript变量
- 正在保存JavaScript内部回调函数中的值
- 有没有什么方法可以停止Jquery中的animate(也可以停止完整的回调函数)
- 如何在javascript中添加带有回调函数的按钮点击事件作为window.conf
- 在Golang回调函数中启用CORS
- 在初始函数完成之前调用回调函数
- jQuery Mobile:如何在$.Mobile.changePage之前运行回调函数
- 即使使用回调函数也无法返回值
- 为什么prototypjs观察到回调函数有绑定