简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM