简体   繁体   中英

Loading thepiratebay npm-module client-side using Browserify

I have a node.js server file that loads my site and runs javascript without any issues. I have added tpb = require('thepiratebay'); to my server.js file and it works fine with the below example in the server file:

tpb.search('Game of Thrones', {
category: '205'
}).then(function(results){
console.log(results);
}).catch(function(err){
console.log(err);
});

I tried using the same code in my html page. In the console I got:

"Uncaught ReferenceError: require is not defined"

So I tried using the browserify module by bundling a simple file with var tpb = require('thepiratebay'); in.

I then called the bundle.js file in my html file.

<script type="text/javascript" src="bundle.js"></script>

But when I try and use the example code tpb.search('Game of Throne...etc I get

"Uncaught ReferenceError: tpb is not defined"

Is it possible to browserify thepiratebay and if so why wont it recognise tpb.search...etc in my html code?

What about putting tpb.search in your server-side code and attach it to the onclick event of a button before you browserify it.

Browserify works by using closures to create scopes. The tbp library and its search function is inaccessible from the global scope, which is where your client-side code is trying to call it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM