2014-05-12 60 views
0

我实际上只是重复了一本书中的代码,但它不会因为某种原因而工作。这是它自己的代码。这很简单nodejs在调用createServer时抛出错误

var express = require('express') 

var app = express.createServer(); 


app.get('/', function(req, res){ 
res.send('Welcome') 
}) 

app.listen(8000) 

当我安装快递。我得到这个屏幕,我认为一切正常

C:\>npm install express 
npm http GET https://registry.npmjs.org/express 
npm http 304 https://registry.npmjs.org/express 
npm http GET https://registry.npmjs.org/type-is 
npm http GET https://registry.npmjs.org/accepts 
npm http GET https://registry.npmjs.org/buffer-crc32 
npm http GET https://registry.npmjs.org/cookie 
npm http GET https://registry.npmjs.org/parseurl 
npm http GET https://registry.npmjs.org/range-parser 
npm http GET https://registry.npmjs.org/cookie-signature 
npm http GET https://registry.npmjs.org/escape-html 
npm http GET https://registry.npmjs.org/serve-static 
npm http GET https://registry.npmjs.org/send 
npm http GET https://registry.npmjs.org/utils-merge 
npm http GET https://registry.npmjs.org/methods 
npm http GET https://registry.npmjs.org/qs 
npm http GET https://registry.npmjs.org/path-to-regexp 
npm http GET https://registry.npmjs.org/merge-descriptors 
npm http GET https://registry.npmjs.org/fresh 
npm http GET https://registry.npmjs.org/debug 
npm http 304 https://registry.npmjs.org/cookie 
npm http 304 https://registry.npmjs.org/parseurl 
npm http 304 https://registry.npmjs.org/accepts 
npm http 304 https://registry.npmjs.org/type-is 
npm http 304 https://registry.npmjs.org/cookie-signature 
npm http 304 https://registry.npmjs.org/buffer-crc32 
npm http 304 https://registry.npmjs.org/range-parser 
npm http 304 https://registry.npmjs.org/send 
npm http 304 https://registry.npmjs.org/utils-merge 
npm http 304 https://registry.npmjs.org/serve-static 
npm http 304 https://registry.npmjs.org/escape-html 
npm http 304 https://registry.npmjs.org/qs 
npm http 304 https://registry.npmjs.org/fresh 
npm http 304 https://registry.npmjs.org/methods 
npm http 304 https://registry.npmjs.org/debug 
npm http 304 https://registry.npmjs.org/path-to-regexp 
npm http 304 https://registry.npmjs.org/merge-descriptors 
npm http GET https://registry.npmjs.org/mime 
npm http GET https://registry.npmjs.org/negotiator 
npm http GET https://registry.npmjs.org/mime 
npm http 304 https://registry.npmjs.org/mime 
npm http 304 https://registry.npmjs.org/negotiator 
npm http 304 https://registry.npmjs.org/mime 
[email protected] node_modules\express 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] ([email protected], [email protected]) 
├── [email protected] ([email protected]) 
└── [email protected] ([email protected], [email protected]) 

当我尝试运行它。我收到错误。好像createServer抛出错误 因为它没有定义什么

C:\Users\User\Dropbox\Personal Documents\Staj node\expresstry>node expresstry.js 


C:\Users\Sinan\Dropbox\Personal Documents\Staj node\expresstry\expresstry.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\Sinan\Dropbox\Personal Documents\Staj node\t 
witter\twitterbuild.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:906:3 

感谢

回答

0

在最新的版本中,它很简单:

var app = express(); 

所以,你的代码是:

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

app.get('/', function(req, res){ 
    res.send('Welcome'); 
}); 

app.listen(8000); 

Source

+0

非常感谢。我应该检查在快递网站的文件,这是我的坏 – sinanspd

+0

@sinanspd不是你的错。几周前,我在尝试在node.js中创建博客上的帖子时遇到了同样的问题。这项技术如此迅速地发展,以至于大多数情况都会立即过时。 – alandarev

0

这本书是根据旧版本express。我会建议寻找更新的代码示例。我把在,因为我知道的版本有createServer - 如果没有问题,但是,你可以用

npm install [email protected] 

如果书中指定的版本的express,用它来代替2.0.0安装旧版本的快车。

请注意,这可能是不稳定的(它希望在节点0.4 - 0.5上运行,这比您可能运行的方式要旧)。你最好找到更新的代码示例或使用不同的材料。

+0

是的,我认为你是对的,找到更多的最新样本会更好,更容易=) – sinanspd

相关问题