Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用nodeJS使imgur链接在url的末尾有.png或.jpg_Javascript_Html_Node.js_Url_Imgur - Fatal编程技术网

Javascript 使用nodeJS使imgur链接在url的末尾有.png或.jpg

Javascript 使用nodeJS使imgur链接在url的末尾有.png或.jpg,javascript,html,node.js,url,imgur,Javascript,Html,Node.js,Url,Imgur,我有一个node JS应用程序,可以从reddit.com/r/wallpaper获取图像 您在html页面上发出请求,它会得到一个随机图像,并将其显示在上 问题是有时您会收到类似http://imgur.com/a/Vtav5不能显示在上,但像http://i.imgur.com/vjUeUVj.jpg可以显示在Node.js上。我如何才能使url成为直接图像url?这是我目前的工作代码 app.get('/', function (req, res) { r.getSubreddi

我有一个node JS应用程序,可以从reddit.com/r/wallpaper获取图像

您在html页面上发出请求,它会得到一个随机图像,并将其显示在

问题是有时您会收到类似
http://imgur.com/a/Vtav5
不能显示在
上,但像
http://i.imgur.com/vjUeUVj.jpg
可以显示在Node.js上。我如何才能使url成为直接图像url?这是我目前的工作代码

app.get('/', function (req, res) {

    r.getSubreddit(subreddit).getRandomSubmission().then(function(post){

        if(post.url.includes(".jpg") || post.url.includes(".png")){
            currentUrl = post.url;
            console.log(currentUrl);
            res.send(currentUrl);
            res.end();

        } else{
            console.log("Not a direct image URL");
            console.log(post.url);
        }

        });
    console.log("Got request");
    //res.end();


})

如果它不能通过他们的API获得,最好的办法就是删除它,希望需要删除的链接不会太多

var Xray = require('x-ray');
var x = Xray();

x('http://imgur.com/a/Vtav5', '.post-images .post-image', [{
  url: 'a@href'
}])(function (err, res) {
    console.log(res);
})
输出:

[ { url: 'http://i.imgur.com/vjUeUVj.jpg' } ]

我遇到了这个恼人的问题,我想你不能在服务器启动和监听时更改VAIRABLE。@ChrisEthanFox你是什么意思?当你运行服务器时,我认为任何变量都不能更改,url是一个变量,需要不断地更改为新的urlvairable@ChrisEthanFox这只是一个例子。您可以将其封装在函数中并使用它。至于xpath,只要imgur不改变它们的html结构,它就会保持不变。