简体   繁体   English

JSON请求返回空主体Node.js

[英]request with JSON returns empty body Nodejs

I'm using request to get a list of movies from yts.ag/api , and display them in grid of thumbnails, when I click on each of the thumbnail I'm taken to another page which display more details of the movie, when I click the thumbnail first time the data is retrieved, the next time when I click the same movie or other movies the body is empty, I'll have to restart the server for it work and it only works with one request after the request returns an empty body . 我正在使用requestyts.ag/api获取电影列表,并将其显示在缩略图网格中,当我单击每个缩略图时,我被带到另一个页面,该页面显示了电影的更多详细信息,当我点击数据被检索缩略图第一次,当我点击了同一部电影或其他电影的下一次body是空的,我将不得不重新启动服务器为它工作,它只是后一个请求工作request退货空的body What seems to be the problem or am I implementing request wrong? 似乎是问题所在,还是我执行request错误?

var express = require('express');
var router = express.Router();
var request = require('request');
var url = 'https://yts.ag/api/v2/movie_details.json';

router.get('/:id', function (req, res, next) {
    url += '?movie_id=' + req.params.id;
    request(url, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            if (body) {
                var importJSON = JSON.parse(body);
                res.render('movie', {title: 'Hypertube - Torrent streaming', moviedetails: importJSON.data.movie});
            }else {
                res.render('movie', {title: 'Hypertube - Torrent streaming', moviedetails: false});
            }
        }
    });
});

module.exports = router;

You are adding the query string containing movie_id to the request URL over and over again. 您一次又一次地将包含movie_id的查询字符串添加到请求URL。 So only the first request will work while all subsequent requests have an invalid request URL. 因此,只有第一个请求有效,而所有后续请求的请求URL均无效。 Instead create a new request URL in your request callback. 而是在您的请求回调中创建一个新的请求URL。 In addition, you just ignore any errors that you might get. 此外,您只需忽略可能会遇到的任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM