2

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._compile (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

Christian
  • 643
  • 1
  • 9
  • 19

2 Answers2

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){
})
3

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 connect@3.3.5, 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 components 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 connect@2.29.1 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 components.

My advice would be to understand why the maintainers of connect decided to move its middleware components into separate modules (hint: connect was becoming large and unwieldy, so to reduce its size and complexity, 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.

Community
  • 1
  • 1
Rob Raisch
  • 15,416
  • 3
  • 43
  • 55
  • `connect.createServer()` refuses to work for me. I still get `undefined is not a function`. –  Apr 14 '15 at 03:39
  • what version of connect are you using? – Rob Raisch Apr 14 '15 at 19:55
  • I'm using version `3.3.5`. –  Apr 15 '15 at 07:35
  • 1
    Ah ha! connect@3.3.5 removed the exported createServer function and instead exports it as the module itself so you can now replace: `var server=connect.createServer();` with `var server=connect();` and you should be good to go. – Rob Raisch Apr 16 '15 at 15:26
  • `connect()` works but `connect.static` still doesn't work. Neither does, `connect().static`. How would I access the `static` function? –  Apr 16 '15 at 18:34
  • Wait, I think I got it. Apparently between 2.X and 3.X versions of connect, there was quite a bit of change. I'm guessing connect no longer provides certain middleware units by default anymore. That's why it's returning undefined. –  Apr 16 '15 at 19:00
  • Ah, I see. If I would have waited a bit, I would have gotten my answer haha. Thanks again! –  Apr 16 '15 at 19:20
  • @user3667018 This should be marked as the answer. I got the same book. Thanks Rob! – onefootswill Jul 08 '15 at 11:56