运行错误的简单Express.js应用程序

时间:2014-05-04 13:53:15

标签: javascript node.js express

我尝试创建简单的快速应用程序serv.js

    var express = require('express'),
    app = express();

    app.listen(3000, function() {
        console.log('Server is running');
    });

当我运行它node serv.js时,在#39中出现错误:

    /home/leonid/proj/first/node_modules/express/lib/application.js:119
  this._router.handle(req, res, function(err) {
               ^
TypeError: Cannot call method 'handle' of undefined
    at Function.app.handle (/home/leonid/proj/first/node_modules/express/lib/application.js:119:16)
    at Server.app (/home/leonid/proj/first/node_modules/express/lib/express.js:28:9)
    at Server.EventEmitter.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2108:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
    at Socket.socket.ondata (http.js:1966:22)
    at TCP.onread (net.js:527:27)

如何解决?

1 个答案:

答案 0 :(得分:1)

您必须为服务器定义路由以响应请求。

var express = require('express'),
app = express();

app.get('/hello.txt', function(req, res){
  res.send('Hello World');
});

app.listen(3000, function() {
    console.log('Server is running');
});