Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 783
Aaron Avatar asked Feb 13 '23 13:02

Aaron


1 Answers

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.

like image 108
maček Avatar answered Feb 15 '23 12:02

maček