Saturday, 15 April 2017

Node.js Request Stream Check if Link broken

I Need to Check if Link was broken here is code i use :

 request(link)
 .on('error', err => console.error(err))
 .on('response', res => {
    if (res.statusCode == 200) {
        console.log('Ok')
    }else {
        console.log('Broken Link')  
    }
    res.req.connection.destroy()    
 })

When I run this code if the link is broken it shows this error and crashes the app.

    events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND doamin.com domain.com:80
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

Normally when I use request like this it's work fine with broken links :

request.get(link,function(err,res,body){
    if (!err && res.statusCode == 200) {
        console.log('Ok Link')  
    }else{
        console.log('Broken Link')
    }
})



via user1086010

No comments:

Post a Comment