2016-08-09 1 views
0

다음 node.js 코드를 호출 할 때마다 동일한 오류가 발생합니다. 이전에 node.js에서 Authorization Headers를 사용하지 않았으므로 잘못 처리해야합니다. 좋은 문서화를 위해 나를 도와 주거나 가르쳐 주시겠습니까? 이 코드는 imgur의 라이브러리에서 'yellow'에 대한 검색으로 검색하려고합니다. 콘솔을 실행할 때마다 항상 동일한 ENOTFOUND 오류가 출력됩니다.Imgur API 호출 : 443 ENOTFOUND 오류 문제 해결

var express = require("express"); 
var moment = require("moment"); 
var http = require("http"); 
var express = require("express"); 
var moment = require("moment"); 
var https = require("https"); 
var mongo = require('mongodb').MongoClient; 

var mystatus=""; 
var app=express(); 

var imgurClientID = "<my id>"; 
var imgurClientSecret = "<my client secret>"; 

var options = { 
    hostname: 'https://api.imgur.com/3/gallery/search?q=yellow', 
    method: "POST", 
    headers:{ 
     Authorization: imgurClientID, 
    } 
} 

app.get('/home', function(req,res){ 
var myreq = https.request(options, function(res){ 
    console.log('Status: '+res.statusCode); 
    console.log('Headers: '+JSON.stringify(res.headers)); 
    res.setEncoding('utf8'); 
    res.on('data', function(body){ 
     console.log("Body: "+body); 
    }); 
}); 
myreq.on('error', function(e){ 
    console.log("Error!: "+e); 
}) 

myreq.end(); 

}); 

// https.get("https://Client-ID:"+imgurClientID+"@api.imgur.com/3/gallery/search?q=yellow", function(res){ 
//  res.setHeader("Client-ID", imgurClientID); 
//  var body = ""; 
//  res.on('data', function(chunk){ 
//   body+=chunk; 
//  }); 

//  res.on('end', function(){ 
//   var imgurRes = JSON.parse(body); 
//   console.log(imgurRes); 
//  }); 
// }).on('error', function(e){ 
//  console.log("Error!: "+e); 
// }) 

app.get('/new/:name*', function(req,res){ 
    //detect if name is a URL 
    //if it is not a proper http, return 'could not load' status 
    if (req.params.name.substr(0,4).toLowerCase() != "http") 
     { 
     mystatus = "Could not load"; 
     res.json({url_part1: req.params.name, 
       url_part2: req.params[0], 
       status: mystatus 
      }); 

     } 
    http.get(req.params.name+req.params[0], function(thisres){ 
     var wwwpath = req.params[0].substr(2, req.params[0].length-2); 

     //do not look for the site unless it returns a 200 status code 
     if (thisres.statusCode != 200){ 
      loadstatus = "Could not load"; 
     } 

     //otherwise, find the site in the MongoDB list! 
     else { 
      //indicate that the site is a working URL 
      loadstatus = "Loaded!"; 

      //prepare the database 
      mongo.connect("mongodb://dickorydock:[email protected]:45365/urlrosetta", function(err, db) { 
       var shorturl = db.collection("shorturl"); 
       //find maximum site number 
       shorturl.find().sort({site_number:-1}).limit(1).toArray(function(err,documents){ 
        max_site_number = documents[0]["site_number"]; 
       }) 
       //look for the site in the existing database 
       shorturl.find(
        {original_url: wwwpath} 
       , {_id: 0, original_url: 1,short_url: 1, site_number:1}) 
       .toArray(function(err,documents){  
        //if we found it, no need to add it again -- just return the existing site number 
        if (documents.length>0){ 
         var sitenumber = documents[0]["site_number"]; 
        } 
        //if we didn't find the working URL, add it 
        else { 
         var sitenumber = max_site_number + 3; 
         var newsitejson={original_url: wwwpath, site_number: sitenumber} 
         shorturl.insert(newsitejson) 
        } 

        //return the info about the short link 
        res.json({original_url: req.params.name+"://"+wwwpath, 
         short_url: "http://dickorydock-shorturl.herokuapp.com/"+sitenumber 
         }); 
        db.close() 
       }) 
      }) 
     } 

    }) 
    //if there is an error in finding the site in the URL, return 'could not load' status 
    .on('error', function(e){ 
     console.error(e.code); 
     res.json({error: "Not a valid URL - try again."})   
    }) 
}) 


app.get('/:shortnum*', function(req,res){ 
    var sitenumber = parseInt(req.params.shortnum); 
    var siteextra = req.params[0]; 
    if (siteextra.length > 0){ 
     res.json({error: "Not a valid reference - try again."}) 
    } 
    else { 
    mongo.connect("mongodb://dickorydock:[email protected]:45365/urlrosetta", function(err, db) { 
     var shorturl = db.collection("shorturl"); 
     //look for the site in the existing database, and either redirect or give an error 
     shorturl.find(
      {site_number: sitenumber} 
     , {_id: 0, original_url: 1, site_number:1}) 
     .toArray(function(err,documents){ 
      if (documents.length>0){ 
      res.redirect("http://"+documents[0]["original_url"]) 
      } 
      else if (documents.length == 0){ 
       res.json({error: "Not a valid reference - try again."}) 
      } 
      db.close() 
     }) 

    }) 

    } 

}) 
app.listen(8080, function(){ 
//app.listen(process.env.PORT, function(){ 
    console.log("App listening") 
}); 

답변

0

시도 :

var options = { 
    hostname: 'api.imgur.com', 
    path: '/3/gallery/search?q=yellow', 
    method: "POST", 
    headers:{ 
     Authorization: imgurClientID, 
    } 
} 

당신이 https://api.imgur.com/3/gallery/search?q=yellow:443을 맞추려고 있기 때문에이 오류가 발생하는 위의 오른쪽 위치로 보내야합니다