admin管理员组文章数量:1398840
Im just going through a tutorial from a book (pro Angularjs) and having some trouble setting up a nodejs webserver.
Like in the book described I use the following server.js to create it:
var connect = require('connect');
connect.createServer(connect.static("../angularjs")).listen(5000);
When im trying to start the server with "node server.js" I get the error:
C:\Programme\nodejs>node server.js
C:\Program Files\nodejs\server.js:2
connect.createServer(connect.static("../angularjs")).listen(5000);
^
TypeError: undefined is not a function
at Object.<anonymous> (C:\Program Files\nodejs\server.js:2:36)
at Module._pile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Any help would be very appreciated,
Greetings, Christian
Im just going through a tutorial from a book (pro Angularjs) and having some trouble setting up a nodejs webserver.
Like in the book described I use the following server.js to create it:
var connect = require('connect');
connect.createServer(connect.static("../angularjs")).listen(5000);
When im trying to start the server with "node server.js" I get the error:
C:\Programme\nodejs>node server.js
C:\Program Files\nodejs\server.js:2
connect.createServer(connect.static("../angularjs")).listen(5000);
^
TypeError: undefined is not a function
at Object.<anonymous> (C:\Program Files\nodejs\server.js:2:36)
at Module._pile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Any help would be very appreciated,
Greetings, Christian
Share Improve this question asked Mar 3, 2015 at 11:40 ChristianChristian 9142 gold badges13 silver badges30 bronze badges2 Answers
Reset to default 4try it
var connect = require('connect');
var app = connect();
app.listen(5000,function(){
console.log('listen on 5k port');
})
app.use('../angularjs', function(req, res){
})
You're receiving this error because at the point you call:
connect.static("../angularjs")
you do not have a valid connect server instance to reference because it is created when you call:
connect.createServer(...).listen(5000);
Before you call connect.createServer()
, connect is a reference to a function and not a server instance with a .static()
function.
You need to create the server instance before you call call any of its functions, as in:
var server=connect.createServer(); // now connect is a server instance
server.static('../angularjs');
server.listen(5000);
See the senchalabs/connect github repo for more about connect.
UPDATE
As of [email protected], connect no longer exports the createServer()
function. Rather the actual export of connect is the createServer function itself.
So instead of calling:
var server=connect.createServer();
you should call:
var server=connect();
which will return the same result as the previous call.
The other (big) change to connect in its 3.X.X release is the extraction of its middleware ponents into separate modules. This is why calling connect.static()
will no longer work.
There are two ways to solve your current problem:
First, assuming you still wish to explore the examples in your, now somewhat outdated, book as they are written, you could npm install [email protected]
which will revert your current version of connect to the last stable version before the new internal reorganization occurred. After you do this, the examples in the book should work as advertised.
Or, you could refactor your example to use the new, modularized middleware modules by installing the required module (using npm install serve-static
) and making the following changes to your code:
var connect=require('connect'),
serveStatic=require('serve-static');
var server=connect();
server.use(serveStatic('../angular.js'));
server.listen(5000);
See Connect Middleware for the list of the now modularized middleware ponents.
My advice would be to understand why the maintainers of connect decided to move its middleware ponents into separate modules (hint: connect was being large and unwieldy, so to reduce its size and plexity, the decision was made to remove parts that were not required by all users and move them into their own modules, thus making them "optional") and how to translate the examples in your book using the same strategy I outline above.
The value of this extra effort will be that, once you've finished the book and understand how to use connect effectively, you won't need to "relearn" everything from the perspective of the new release.
本文标签: javascriptnodejs webserver quotundefined is not a functionquotStack Overflow
版权声明:本文标题:javascript - nodejs webserver "undefined is not a function" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744643976a2617287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论