Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

javascript - Simple Express.js application running with error

i try to create simple express apllication serv.js:

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

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

and when i run it node serv.js, i've got error:

    /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)

How can it resolve?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to define a route for the server to respond to requests.

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');
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...