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!
Change:
.use(app.static(path.join(__dirname, '/public')));
To:
.use(express.static(path.join(__dirname, '/public')));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With