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

2

7

when I call this node.js file

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

I 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?

Champaign answered 20/6, 2014 at 23:18 Comment(0)
C
13

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);
Champaign answered 21/6, 2014 at 0:18 Comment(5)
this should be in documentation... sad is that it is not the case :(Juneberry
has been added now, at least here: github.com/senchalabs/connect/blob/master/Readme.mdChampaign
I guess they are still in the process of updating the docs, still not updated here: senchalabs.org/connectChampaign
For the project I'm working with, I needed to change serveStatic('public') to serveStatic(__dirname)Sachs
Related: I also had to install gulp-serve-index and replace connect.directory('app') with serveIndex('app')Danais
I
1

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);
Izettaizhevsk answered 25/3, 2015 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.