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

For a while I searched Google to find the answer to this question, but I still cannot find this question in my code. I am trying to make my first Socket.io application, which simply displays a message to the console when the user connects.

Here is 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);
Run codeHide result

Thanks in advance for your advice!

+4
source share
2 answers

Edit:

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

To:

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

+9
source

, . express-static, "npm install -g express-static -save". , ,

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

, . https://www.npmjs.com/package/express-static

+1

All Articles