4

I'm trying to launch my node server using an example from an AngularJS book (server.js).

var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);

Initially I was getting "object has no method static" so I re-installed the connect include and now when I do: node server.js I get a blinking cursor in CMD (Windows) and "Cannot GET /" from my browser.

Any ideas folks?

Thanks!

Michael Harper
  • 1,389
  • 1
  • 20
  • 36
  • 1
    Hi, I found that article and used the suggested code - I still get "Cannot GET /" – Michael Harper Jun 01 '14 at 10:23
  • 24
    I think you are reading the same book as me. The connect module has been reorganized. do npm install connect and also npm install serve-static. Afterward your server.js can be written as: var connect = require('connect'); var serveStatic = require('serve-static'); var app = connect(); app.use(serveStatic('../angularjs')); app.listen(5000); – KnarfaLingus Jun 21 '14 at 22:43
  • 1
    @KnarfaLingus this works for me thanks! and yes, we are reading same book :) – Teoman shipahi Jul 04 '14 at 04:25
  • Reading the same book. Adding it to the ERRATA for the book on Apress – Chris Knight Feb 07 '15 at 22:11
  • The issue I had with this was that when entering http://localhost:PORTNUM in my browser, I had to follow this with the name of the file, e.g. http://localhost:8000/test.html. Otherwise, yes, the default file it looks for is index.html, in our case for this book, in the /angularjs folder – thehme Apr 01 '15 at 02:09

1 Answers1

5

Your application is working just fine. You just need to specify the name of the file you want to access from static folder in the URL. For example if you have a file called app.html you need to access it via:

http://localhost:5000/app.html

Note that if you just use root URL, it will cause connect to look for default file name, which defaults to index.html. You can change that by passing new default file name in options:

connect.static("../angularjs", {default: "app.html"});
jsalonen
  • 26,663
  • 14
  • 82
  • 104
  • Weird, I was getting the same "cannot get" error until I put the server.js file into the angularjs folder itself - then it all worked! It's like it can't drill down in to angularjs folder for some reason – Michael Harper Jun 01 '14 at 19:01
  • 2
    As of connect@3.X.X, the static middleware has been moved into a separate module. See the update to http://stackoverflow.com/questions/28830902/nodejs-webserver-undefined-is-not-a-function/28844402?noredirect=1#comment47503310_28844402 – Rob Raisch Apr 16 '15 at 19:08