3

I try to share my code betwen server and client I use following code (app.js):

var io = require('socket.io').listen(8000), 
  Static = require('socket.io').Static; 

io.configure(function () {
  var _static = new Static(io); 

  // some methods to add my custom files 

  _static.add('\public\test.js');
  io.set('static', _static);
});

My file structure looks like this:

  1. root
    • app.js
    • public
      • test.js

When I type "http://localhost:8000/public.test.js" Browser download default file "Welcome to socket.io"

1 Answer 1

7

This question is rather old, but here's the current way to do it (for v0.9):

var io = require('socket.io').listen(8000);
io.static.add('/path/for/request.js', {file: 'path/to/file.js'});

Note that the path to the resource is relative to the socket.io path, so the request URI would be something like:

http://localhost:8000/socket.io/path/for/request.js

If you see an error like Protocol version not supported, then that means your request URI probably has an extension that the manager can't support. Here's how to add that support:

io.static.add('/path/for/request.foo', {
  mime: {
    type: 'application/javascript',
    encoding: 'utf8',
    gzip: true
  },
  file: 'path/to/file.js'
});

The documentation points at their own Static library for a working implementation.

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

5 Comments

damn, Just read the first 3 lines & spent 60 minutes on, "why the path is not working!!!" ewww..
how do you serve all the files in a folder?
i am not able to find documentation for io.static.add. could you point me one?
The repo has been updated for the "upcoming" 1.0 release, and the documentation is in the readme. For v0.9, to which this answer refers, the wiki points at their own implementation
the 'path/to/file.js' seems to be relative to some socket.io directory I could not find. If you use __dirname + '/path/to/file.js' instead this becomes relative to your current js-file (most likely your app.js)

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.