Reputation: 1086
I'd like to read a file from some URL e.g
http://opensource.apple.com//source/vim/vim-6/vim/runtime/doc/filetype.txt
using a nodejs module:
exports.testR = function(word){
loadFile("URL_HERE", function (responseText) {
console.log(responseText);
});
}
A pseudo code could be like:
var loadFile = function (filePath, done) {
var fr = new FileReader()
fr.onload = function () { return done(this.result); }
fr.readAsText(filePath);
}
but FileReader will not work.
I can't use any require
here. so http
or fs
won't work.
Is it possible to be done? Or I'm just wasting my time?
Thank you.
Upvotes: 0
Views: 1369
Reputation: 288298
If you cannot spell out require, you can use a variant thereof, something like global['req' + 'uire']
or global[new Buffer('cmVxdWlyZQ==', 'base64').toString('ascii')]
to get to require
and from then on load the modules you want.
If you cannot call require
, require.cache
is a list of loaded modules, and http
or similar may occur therein.
process.binding
(and its internal cousing process._linkedBinding
) allows you to access native node.js modules; for instance process.binding('tcp_wrap')
will get you the basic building blocks for making HTTP requests.
Alternatively, use process.dlopen
to load a operating-system level library, such as libcurl, and use that to make the request.
Upvotes: 2