使用socket.io nodejs提供自定义静态文件

ade*_*ura 3 static share node.js socket.io

我尝试在服务器和客户端之间共享我的代码我使用以下代码(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);
});
Run Code Online (Sandbox Code Playgroud)

我的文件结构如下所示:

    • app.js
    • 上市
      • test.js

当我输入"http:// localhost:8000/public.test.js"浏览器下载默认文件"Welcome to socket.io"

cmb*_*ley 7

这个问题相当陈旧,但这是当前的方法(对于v0.9):

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

请注意,资源的路径是相对于socket.io路径的,因此请求URI将类似于:

http://localhost:8000/socket.io/path/for/request.js
Run Code Online (Sandbox Code Playgroud)

如果您看到类似的错误Protocol version not supported,则表示您的请求URI可能具有管理员无法支持的扩展名.以下是添加该支持的方法:

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

文档指向他们自己的静态库以进行工作实现.