Imgur API调用:443 ENOTFOUND错误疑难解答

时间:2016-08-09 12:24:42

标签: javascript node.js request imgur

每次调用以下node.js代码时,我都会收到相同的错误。我之前没有在node.js中使用过Author Header,所以我一定做错了。帮助或指出我的文档?代码正在尝试检索“黄色”'在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")
});

1 个答案:

答案 0 :(得分:0)

尝试:

$USERLIST = (Get-WmiObject -Class Win32_Process -Filter "Name='tsappldr.exe'" -ComputerName "SRUS270137C").getowner().user
ForEach ($computer in $USERLIST) {
$CmdMessage = {C:\windows\system32\msg.exe   $computer  /v 'Cancel your session'} $CmdMessage | Invoke-Expression 
}
Start-Sleep -m 5  #time-out for 5 minutes

get-process tsappldr, tpedrte  | stop-process -force

您收到该错误是因为它试图点击https://api.imgur.com/3/gallery/search?q=yellow:443,上述内容应该将其发送到正确的位置