Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

express.static(): "Undefined is not a function"

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!

like image 405
Drav' Avatar asked Apr 20 '15 13:04

Drav'


2 Answers

Change:

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

To:

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

like image 144
Adrian Lynch Avatar answered Sep 30 '22 05:09

Adrian Lynch


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

like image 22
jrhamza Avatar answered Sep 30 '22 06:09

jrhamza