Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs connect usage of built in modules -> method not found

when I call this node.js file

var connect = require('connect');
var app = connect();
app.use(connect.static('public'));
app.listen(3000);

I do immediately get

app.use(connect.static('public'));
                      ^
TypeError: Object function createServer() {
  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'

Using Connect 3.0.1, are there changes with the integrated modules? If yes, how does it work then?

like image 1000
ohoservices Avatar asked Jun 20 '14 23:06

ohoservices


2 Answers

big changes coming with connect 3: middleware modules not included any longer. Find them at github.com/expressjs. "static" is now "serve-static". It needs to be installed separately with:

npm install serve-static

The above code should now look like this:

var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic('public'));
app.listen(3000);
like image 100
ohoservices Avatar answered Oct 06 '22 00:10

ohoservices


I had to install connect and serve-static

npm install connect

then type:

npm install serve-static

The code below will give you a nice message telling you your server is connected to port 3000.

var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
var port = 3000;
app.use(serveStatic(__dirname));
app.listen(port);
console.log('You are connected at port '+port);
like image 43
Ian Poston Framer Avatar answered Oct 05 '22 23:10

Ian Poston Framer