what does var io = require('../..')(server) do?

流过昼夜 提交于 2020-01-02 06:32:10

问题


I've build the project https://github.com/Automattic/socket.io/tree/master/examples/chat locally and it is working great. However, it would be nice to understand a little more about how a socket application works.

In the main startup script one of the modules that is pulled in with require is

var io = require('../..')(server)

what does require('../..') do?

thanks!


回答1:


When a path to a directory is given to require, it will implicitly look for an index.js in that directory.

In this case, it's the equivalent of

var socket = require("../../index.js");
var io     = socket(server);

In the example provided, they're just using some shorthand and throw away the intermediate value returned by the call to require.

Check out the module.require docs for more info.




回答2:


Here, in your code

require('../..');

Will add File form the path, which have used SOCKET.IO, as you can see that you have not added Socket.io module.

Also, if no specific path give for file or folder, Module require will try to load index.js or index.node. if no such file exist then it will give error.



来源:https://stackoverflow.com/questions/24668755/what-does-var-io-require-server-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!