jtrussell
jtrussell

Reputation: 695

NodeJS - use remote module?

I'm working with node and would like to include a module stored on a remote server in my app.

I.E. I'd like to do something along these lines (which does not work as is):

var remoteMod = require('http:// ... url to my remote module ... ');

As a workaround I'd be happy with just grabbing the contents of the remote file and parsing out what I need if that's easier - though I haven't had much luck with that either. I have a feeling I'm missing something basic here (as I'm a relative beginner with node), but couldn't turn up anything after scouring the docs.

EDIT:

I own both local and remote servers so I'm not concerned with security issues here.

If I'm just going to grab the file contents I'd like to do so this synchronously. Using require('http').get can get me the file, but working from within the callback is not optimal for what I'm trying to do. I'd really be looking for something akin to php's fopen function - if that's even doable with node.

Upvotes: 3

Views: 4226

Answers (2)

Emmerman
Emmerman

Reputation: 2343

You can grab remote file just via http http://nodejs.org/docs/v0.4.6/api/http.html#http.get

require('http').get({host: 'www.example.com', path: '/mystaticfile.txt'}, function(res) {
 //do something
});

Upvotes: 2

Van Coding
Van Coding

Reputation: 24534

Running code loaded from another server is very dangerous. What if someone can modify this code? This person would be able to run every code he wants on your server.

Upvotes: 5

Related Questions