2

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 combinations to get both what I read on the net regarding including static css and js and serving httpServer instance to Socket.IO.

Thanks :)

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Tried it, says "express is not defined"
@dnmh Try with the added requires. It's necessary to have the entire express module defined, not just the initiated portion (app)
Now: "path is undefined"
@dnmh I need to stop assuming requires - sorry. Try with that new require statement.
Yup, that's it :) Thank you very much, now I can work normally :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.