我无法使用express框架解决nodejs中的错误

时间:2014-04-14 21:58:52

标签: node.js express

我是node.js和express框架的初学者。我正在关注这些tutorials

这是我的代码:

var fs = require("fs");
var config = JSON.parse(fs.readFileSync(__dirname + "\\config.json"));
var host = config.host;
var port = config.port;
var express = require("express");

var app = express.createServer();

app.get(__dirname + "\\",function(request, response){
    response.send("Hello!");
});

app.listen(port,host);

我遇到了这个错误,我无法解决它:

C:\Users\Fabio\Documents\GitHub\NodeJSProjects\ExpressSimpleServer\Server.js:7
var 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> (C:\Users\Fabio\Documents\GitHub\NodeJSProjects\ExpressSimpleServer\Server.js:7: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

我正在使用node.js 0.10.26,表达4.0.0和Windows 8

2 个答案:

答案 0 :(得分:6)

参见明确项目的参考。不推荐使用CreateServer。只需执行此操作即可创建服务器并侦听端口 Var app = require(“express”)(); app.listen(3000);

答案 1 :(得分:1)

<强>解决
我根据expressjs指南更改代码

var fs = require("fs");
var config = JSON.parse(fs.readFileSync(__dirname + "\\config.json"));
var host = config.host;
var port = config.port;
var express = require("express");
var app = express();
app.get('/hello.txt', function(req, res){
    res.send('Hello World');
});
var server = app.listen(port, function() {
    console.log('Listening on port %d', server.address().port);
});