I\'ve been working on an online socket server using NodeJS and javascript, and I\'ve been creating \"playrooms\" in my code using require:
new_game_obj = req
How about the 'require from URL' NPM package?
Just found it - not tried it, yet! https://www.npmjs.com/package/require-from-url
Specifically, refer to the require algorithm
Within node.js, require
calls are synchronous, so it's not possible to load files that are not on your file system (ie, from an external url).
You could fetch the code through an http request - or, even better, an https request and run it with the built-in vm module - or even with eval
, but that seems not a good idea - as suggested on this old question.
Something like
https.get( 'https://www.xxxxx.com/blabla/forza4.js', function( res ){
res.on( 'data', function( data ){
vm.runInThisContext( data, 'remote/forza4.js' );
});
});
Note: I did not test this code
Sure it isn't the best solution, but is a solution.