123

A newbie with NodeJs. I am trying to follow AngularJS pro and got stuck with setting up NodeJs server. According to book, I installed nodejs and then installed connect package using npm install connect.

Then downloaded angularjs in folder next to nodejs folder. Then wrote server.js file to connect to server. Here is the content of the file:

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

When I run this server.js file using:

node server.js

I get following error:

 function app(req, res, next){ app.handle(req, res, next); }
 merge(app, proto);
 merge(app, EventEmitter.prototype);
 app.route = '/';
 app.stack = [];
 return app;
 has no method 'static'
   at Object.<anonymous> (C:\web\nodejs\server.js:2:36)
   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

NOTE: I have tried other solution given here but it didn't work

Any ideas?

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Sheraz
  • 2,177
  • 4
  • 18
  • 21

5 Answers5

223

The connect package has made some changes in the latest 3.x version of their code base, moving the static middleware to it's own package. You can view the list of packages that have been moved here.

So you have two options:

Option 1
You can install an older, 2.x version of connect and use that as is:

$ npm install connect@2.X.X

Installing the latest 2.X.X version will allow your current implementation to function properly.

Option 2
You can continue to use the 3.x version of connect, and also add serve-static:

$ npm install serve-static

You would also have to update your server.js file to include the new serve-static module:

var connect = require('connect'),
    serveStatic = require('serve-static');

var app = connect();

app.use(serveStatic("../angularjs"));
app.listen(5000);
dylants
  • 22,316
  • 3
  • 26
  • 22
  • 5
    worked for me by using single dot in case of same folder i.e. ./angularjs – coure2011 Sep 24 '14 at 07:12
  • @coure2011 This all depends on your `pwd` when you start node. Using two dots is correct when running `node server.js` when starting from `/angularjs` (presumably your pwd is `/angularjs/..`) – Michael Aug 13 '15 at 19:06
  • I tried this , now I am getting error in browser "cannot get /" – Ans Bilal Nov 16 '17 at 17:48
  • @AnsBilal try "localhost:5000/test.html". Assuming you are following the example in the book (referenced in the OP) that URL should work for you. – akvallejos Jan 02 '18 at 21:29
  • yes i was trying an example from the book (Pro AngularJs by Adam Freeman). After lit bit search I came to solution and I posted that on my blog for others also : https://anscoding.blogspot.com/2017/11/installing-web-browserpro-angularjs-by.html – Ans Bilal Jan 06 '18 at 16:38
9

The answer by dylants is helpful. However, here are the exact steps that I followed in order to resolve the same error. 1. From the command window , change directory to the directory in which you installed nodeJS. 2. After having already ran npm install connect, run:

npm install serve-static

3. Create a file named server.js with the following code:

var connect = require('connect'),
serveStatic = require('serve-static');

var app = connect();

app.use(serveStatic("./angularjs"));
app.listen(5000);
  1. While still in command window, and still in the directory in which you have installed nodeJS, run:

    node server.js

  2. Navigate to the URL http://localhost:5000/test.html

This should work. Here is my directory configuration: C:\NodeJSInstallLocation\angularjs

Judy007
  • 5,484
  • 4
  • 46
  • 68
1
var connect = require('connect'),
    serveStatic = require('serve-static');

connect().use(
    serveStatic("angularjs")
).listen(5000);
  • 4
    Welcome to SO. Please read up on http://stackoverflow.com/help/how-to-answer. Your answer does not really give any additional insight to the accepted answer. – Jaques May 25 '15 at 21:48
1

You might want to try something like this

var express = require('express');
var app = express();
app.use(express.static('angularjs'));
Gautam Anand
  • 19
  • 1
  • 3
0

Try this...

  const express = require('express');
  const app = express();
  const path = require('path');

  app.use('',express.static(path.join(__dirname, '/static/')));
Alexander mit
  • 11
  • 1
  • 1
  • 3