javascript - Serve static files with Node.js & Socket.IO - Stack Overflow

Although I already browsed some answers both here on SO and the net, I didn't find what I was look

Although I already browsed some answers both here on SO and the net, I didn't find what I was looking for. I am also a newb in Node.js so perhaps that's the problem.

This is the code that I have and need for starting Node and Socket.IO:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

What I need next would be something like this:

http.use(app.static(__dirname + "/public"));

"app has no method 'static' " is my problem. I tried several other binations to get both what I read on the net regarding including static css and js and serving httpServer instance to Socket.IO.

Thanks :)

Although I already browsed some answers both here on SO and the net, I didn't find what I was looking for. I am also a newb in Node.js so perhaps that's the problem.

This is the code that I have and need for starting Node and Socket.IO:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

What I need next would be something like this:

http.use(app.static(__dirname + "/public"));

"app has no method 'static' " is my problem. I tried several other binations to get both what I read on the net regarding including static css and js and serving httpServer instance to Socket.IO.

Thanks :)

Share Improve this question asked Jul 20, 2014 at 15:51 dnmhdnmh 2,1352 gold badges37 silver badges53 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Try this:

var express = require('express');
var path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));

express.static is the method you're looking for, not app.static, though they would seem identical.

Also, see this for an example of an application using both socket.io and express. Note that they only use the http server for socket.io, not serving the web pages.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744227357a4564073.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信