5

Been Googling for a while in order to find an answer to this question but I still can't get to find the matter in my code. I'm trying to make my very first Socket.io app that simply outputs a message to the console when a user connects.

Here's the error I get from Node.js:

And here is my source code:

var port = process.argv[2];

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

app.get('/', function(req, res) {
  res.render('index.ejs');
})
.use(app.static(path.join(__dirname, '/public')));

var io = require('socket.io')(app.listen(port));

io.on('connection', function(socket) {
  console.log('Someone connected');
});

console.log('Listening on port ' + port);

Thanks in advance for your advice!

Drav'
  • 105
  • 1
  • 6
  • 2
    Well, [`app` doesn't have a method `static`](http://expressjs.com/api.html#app). What did you expect that to do? Perhaps you meant [`express.static`](http://expressjs.com/api.html#express.static)? – deceze Apr 20 '15 at 13:41

2 Answers2

12

Change:

.use(app.static(path.join(__dirname, '/public')));

To:

.use(express.static(path.join(__dirname, '/public')));

Adrian Lynch
  • 8,237
  • 2
  • 32
  • 40
  • Well, sometimes the dumbest errors are the hardest to find when they're right under your nose, "hidden" in an 18-lines code haha. I feel kinda stupid now but yes, thanks a lot! – Drav' Apr 20 '15 at 16:25
  • 2
    There's no need to feel silly. We all do it! I'm debugging an issue with an npm package I wrote at the moment. All the tests pass but it fails in production! It'll be something silly I'm sure of it... – Adrian Lynch Apr 21 '15 at 00:10
2

I tried all the answers above, but didnt work for me. Then I tried to install express-static using "npm install -g express-static --save". And used in the code like below,

var pathComp= require("express-static");
app.use(pathComp(__dirname+"/client"));

Sharing this though its an old thread. For more reference visit, https://www.npmjs.com/package/express-static

jrh
  • 764
  • 13
  • 31
  • That is because those answers are old. It was correct answer at that time, but now `static` is a different module and not a part of `express`. – a1626 Dec 29 '17 at 06:53