tomas / needle

Nimble, streamable HTTP client for Node.js. With proxy, iconv, cookie, deflate & multipart support.

Home Page:https://www.npmjs.com/package/needle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

repeats twice for some reason

redimongo opened this issue · comments

So I am trying to do something simple which is use needle.get to fetch a pre-roll station ID before it plays the podcast.

It works fine. However here is the issue. according to the logs it is running the code twice.
I am wondering what am I doing wrong as I would like to be able to use the .on('end') to store a database endtime - but i don't need to it run twice.

STATION_ID Completed
754542
STATION_ID Stream END
get POdcast ran
get data connected!
63062853
STATION_ID Completed
754542
STATION_ID Stream END
get POdcast ran
get data connected!
63062853
router.get('/track/:url(*)', (req, res) =>{
    var url = req.params.url.substr(0); 
    var length = 0;
    var clients = [];
    var e = 0;
    /* AD SYS */
    var remote = "https://storage.googleapis.com/ad-system/testfolder/OUTOFAREA.mp3";  
    var adsys = needle.get(remote)
    /* PODCAST */
    var filesize = needle.get(url, function(error, response, body) {
       if(error){
        e = 505;
        res.send("<html><head></head><body>NOPE</body></html>");
        console.log(error)
        //filesize.end();
        res.end();
        
       }
    });

   
        adsys.on('response', function(res) {
            console.error("STATION_ID Completed");
            length =  Number(res.headers['content-length']);
            console.log(length);           
        });

        filesize.on('response', function(res) {
                console.error("get data connected!");
                console.log(res.headers['content-length']);
                a = Number(res.headers['content-length']);
                length = length+a;
                
      });
      res.set({
        "Content-Type": "audio/mpeg",
        'Transfer-Encoding': 'chunk',
    //    'Content-Disposition': 'attachment',
    //    'Content-Length':length
    });

      adsys.on("end", function() {
        
        console.error("STATION_ID Stream END");
       
        getPodcast();
     
        
        //res.end();
    });  


    adsys.on("data", function (chunk) {
            // console.log(clients.length);
            if (clients.length > 0){
                for (client in clients){
                    clients[client].write(chunk);
                    //console.log(chunk);
                };
            }
        
    });

    function getPodcast(){
        console.log("get POdcast ran");
        filesize.on("data", function (chunk) {
            // console.log(clients.length);
            if (clients.length > 0){
                for (client in clients){
                    clients[client].write(chunk);
                    //console.log(chunk);
                };
            }
            
        });
    }
    clients.push(res);
});`
```

Try changing adsys.on('end', fn) to adsys.on('done', fn)

Try changing adsys.on('end', fn) to adsys.on('done', fn)

I tried this and it still ran twice.
STATION_ID Completed X STATION_ID Stream END get POdcast ran get data connected! 63062853 STATION_ID Completed X STATION_ID Stream END get POdcast ran get data connected!

I'm not sure what you're trying to do, but I just ran your code (removing the res calls) and it ran only once:

STATION_ID Completed
754542
get data connected!
undefined
STATION_ID Stream END
get POdcast ran

Are you sure the request isn't being triggered twice? Try logging whenever the '/track/:url(*) route gets called.

I'm not sure what you're trying to do, but I just ran your code (removing the res calls) and it ran only once:

STATION_ID Completed
754542
get data connected!
undefined
STATION_ID Stream END
get POdcast ran

Are you sure the request isn't being triggered twice? Try logging whenever the '/track/:url(*) route gets called.

So I took your advice and added

router.get('/track/:url(*)', (req, res) =>{
    var url = req.params.url.substr(0); 
    console.log('/track/'+url);

I figured out the line that is causing the issue:
clients.push(res);

I tried removing the res from that line and it wont load the file in browser or to the user. Any suggestions?

Not that I think of now, sorry. :)