节点https无法解析URL

时间:2017-01-30 08:30:53

标签: node.js

我有一个小问题,希望你能帮助我。 我们曾经有一个开发人员,他重构了一些代码,现在却无法正常工作。

const request = require('request'),
  API_KEY = '',
  https = require('https');

class ImageService {

    getActorImage(name) {
      return new Promise((resolve, reject) => {
        request.get(`https://api.themoviedb.org/3/search/person?api_key=${API_KEY}&language=en-US&query=${name}`, (error, response, body) => {
        if (error) {
          // todo: handle the error
          reject(error);
        }

        // todo: what if there are multiple results?
        var bodyAsObject = JSON.parse(body);
        var actor = bodyAsObject.results[0];

    https.get(`https://image.tmdb.org/t/p/w300_and_h450_bestv2${actor.profile_path}`, function (profileResponse) {
          var image_data = '';
          profileResponse.setEncoding('binary');

          profileResponse.on('data', function (chunk) {
            image_data += chunk
          });

          profileResponse.on('end', function () {
            resolve(image_data)
          });
        });
      });
    });

    }


    getBackdropImage(backdropPath) {
        return new Promise((resolve) => {
          https.get(`https://image.tmdb.org/t/p/original${backdropPath}`, function (backdropResponse) {
        var image_data = '';
        backdropResponse.setEncoding('binary');

        backdropResponse.on('data', function (chunk) {
          image_data += chunk
        });

        backdropResponse.on('end', function () {
          resolve(image_data)
        });
      });
    });
    }

    getPosterImage(posterPath) {
    return new Promise((resolve) => {
      https.get(`https://image.tmdb.org/t/p/original${posterPath}`, function (posterResponse) {
        var image_data = '';
        posterResponse.setEncoding('binary');

        posterResponse.on('data', function (chunk) {
          image_data += chunk
        });

        posterResponse.on('end', function () {
          resolve(image_data);
        });
      });
    });
    }

    getPosterAndBackdropPath(imdbid) {
    return new Promise((resolve) => {
      request.get(`https://api.themoviedb.org/3/find/${imdbid}?api_key=${API_KEY}&language=en-US&external_source=imdb_id`, (error, response, body) => {
        var responseAsObject = JSON.parse(body);
        var movie = responseAsObject.movie_results[0];
        var posterPath = movie.poster_path;
        var backdropPath = movie.backdrop_path;
        resolve({ poster_path: posterPath, backdrop_path: backdropPath });
      });
    });
    }

}

module.exports = new ImageService();

现在我的问题是,即使actor.profile_path中有数据,它也不会将其解析为实际路径。对于使用$ {}的其他所有内容都是一样的。

如果你能在这里帮助我,我们将非常感激

更新

现在已经在Windows,Mac和Ubuntu上测试了这个 它只适用于Mac

有什么想法吗?

提前致谢

托马斯

1 个答案:

答案 0 :(得分:0)

${}的字符串需要包含在`反引号)中。

例如:

https://api.themoviedb.org/3/search/person?api_key=${API_KEY}&language=en-US&query=${name}

应更改为(注意开头和结尾的反引号符号):

`https://api.themoviedb.org/3/search/person?api_key=${API_KEY}&language=en-US&query=${name}`

另一个例子:

https://image.tmdb.org/t/p/original${posterPath}

应更改为:

`https://image.tmdb.org/t/p/original${posterPath}`