【问题标题】:Imgur API call: 443 ENOTFOUND error troubleshootingImgur API 调用:443 ENOTFOUND 错误排查
【发布时间】:2016-12-15 11:55:01
【问题描述】:

每次调用以下 node.js 代码时都会收到相同的错误。我之前没有在 node.js 中使用过授权标头,所以我一定是做错了什么。帮助或指出我的好文档?该代码正在尝试检索 imgur 库中的“黄色”搜索。我的控制台每次尝试运行时总是打印相同的 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:$iderHouseRul3z@ds145365.mlab.com: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:$iderHouseRul3z@ds145365.mlab.com: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")
});

【问题讨论】:

    标签: javascript node.js request imgur


    【解决方案1】:

    试试:

    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,上面应该将它发送到正确的位置

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 2019-10-28
      • 1970-01-01
      • 2017-06-06
      • 2015-11-01
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多