最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - express.static(): "Undefined is not a function" - Stack Overflow

programmeradmin2浏览0评论

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);

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!

Share Improve this question asked Apr 20, 2015 at 13:39 Drav'Drav' 1051 silver badge7 bronze badges 1
  • 2 Well, app doesn't have a method static. What did you expect that to do? Perhaps you meant express.static? – deceze Commented Apr 20, 2015 at 13:41
Add a ment  | 

2 Answers 2

Reset to default 12

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./package/express-static

发布评论

评论列表(0)

  1. 暂无评论