https://github.com/feross/webtorrent#usageに示されている例に問題があり ます。ブラウザーでコードを使用しようとしています。そこで、最初に app.js というファイルを作成します
app.js
var WebTorrent = require('webtorrent')
var concat = require('concat-stream')
var client = new WebTorrent()
console.log('Hi there');
client.download('magnet:?xt=urn:btih:XXXXXXXX', function (torrent) {
// Got torrent metadata!
console.log('Torrent info hash:', torrent.infoHash)
torrent.files.forEach(function (file) {
// Get the file data as a Buffer (Uint8Array typed array)
file.createReadStream().pipe(concat(function (buf) {
// Append a link to download the file
var a = document.createElement('a')
a.download = file.name
a.href = URL.createObjectURL(new Blob([ buf ]))
a.textContent = 'download ' + file.name
document.body.appendChild(a)
}))
})
})
次に、コマンドを入力browserify app.js > bundle.js
して、ブラウザでコードを機能させることができます。index.html という別のファイルを作成します。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test</title>
<script src="bundle.js"></script>
</head>
<body id="home">
<h1>test</h1>
</body>
</html>
コンソールからは「こんにちは」しか見えません。client.download() 関数が機能しなかったようです。なぜこれが起こったのですか?私は browserify を初めて使用します。使用しているコマンドに問題はありますか?