0

Is it possible to include files like this? I need write a lot of code and it could help me with clarity. P.S. Sorry for my English ;)

io.sockets.on('connection', function(client) {
// include file1.js
// include file2.js
});

Content of file1.js

client.on('event1',function() {
// do something here...
});

Content of file2.js

client.on('event2',function() {
// do something here...
});

1 Answer 1

1

If you don't need to conditionally load the JavaScript files, you could do something like this:

main.js

io.sockets.on('connection', function(client) {
  setup1(client);
  setup2(client);
});

file1.js

function setup1(client) {
  client.on('event1',function() {
  // do something here...
  });
}

file2.js

function setup2(client) {
  client.on('event2',function() {
  // do something here...
  });
}

And of course you could make it all cleaner by using a library like Postal.js to handle inter-application communication.

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

1 Comment

Thanks, it works :) btw: can I specify additional value for client? e.g. client.name=nickname?

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.