2014-09-19 38 views
0

不能找到方法,我有节点的js代码:在快递

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

app.use(express.bodyParser()); 

app.post('/', function(request, response){ 
    console.log(request.body);  // your JSON 
    response.send(request.body); // echo the result back 
}); 

app.listen(3000); 

,当我跑我得到这样的错误:

, app = express.createServer(); 
       ^
TypeError: Object function createApplication() { 
    var app = function(req, res, next) { 
    app.handle(req, res, next); 
    }; 

    mixin(app, proto); 
    mixin(app, EventEmitter.prototype); 

    app.request = { __proto__: req, app: app }; 
    app.response = { __proto__: res, app: app }; 
    app.init(); 
    return app; 
} has no method 'createServer' 
    at Object.<anonymous> (/Users/anto_belgin/programs/node/appleconnect.js:2:19) 
    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) 
    at Function.Module.runMain (module.js:497:10) 
    at startup (node.js:119:16) 
    at node.js:902:3 

我已经安装使用express

npm install express 

有什么我需要做的让它工作?

+0

注:'express.createServer()'是[弃用与3.X(https://github.com/ strongloop/express/wiki/Migrating-from-2.x-to-3.x#changed)和[用4.x删除](https://github.com/strongloop/express/wiki/Migrating-from-3 .X到4.x的#expresscreateserver)。 – 2014-09-19 05:20:37

回答

2

在我的Express 4的应用程序,我这样做:

var express = require('express'); 
var app = express(); 
var server = app.listen(8081, function() {}); 

由于快递改了一下,从V3到V4,确保你看你正在运行的版本的说明。

我认为createServer()是你在http模块上做的事情,而express是在更高级别上运行的,所以如果你使用express模块​​启动服务器,你可以做不同的事情。

从Express文档,app.listen()是一个方便的方法来替换这样的:

app.listen = function(){ 
    var server = http.createServer(this); 
    return server.listen.apply(server, arguments); 
}; 
+0

是的,你的正确,版本不匹配在这里。 – batman 2014-09-19 05:09:19

+1

我刚刚学会表达自己,发现很难理清网络上哪些版本的随机文章是为了写作而编写的。我现在对socket.io库做了同样的事情,它也有两个非常不同的版本。这很容易被绊倒。 – jfriend00 2014-09-19 05:11:59

+0

是的。其实,它很糟糕。 – batman 2014-09-19 05:12:37