Node Js .get()需要回调函数

时间:2014-10-15 08:17:14

标签: javascript node.js

这是我的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)

当我运行节点server.js文件时,我遇到了上述错误。它工作得更早了。我无法解决这个问题。请任何人帮助我。

1 个答案:

答案 0 :(得分:0)

错误说明了一切,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()

});

此外,不确定是否是由于您所做的copypaste,但第一个分号require('express');应该是逗号。