最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - nodejs webserver "undefined is not a function" - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

try 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.

发布评论

评论列表(0)

  1. 暂无评论