簡體   English   中英

nodejs 獲取 API 返回相同的 JSON 文件

[英]nodejs fetch API returning the same JSON file

我正在嘗試通過構建自己的 rest API 來學習 nodejs。

我從https://sweetalert.js.org/guides/#getting-started復制了這段代碼:

swal({
  text: 'Search for a movie. e.g. "La La Land".',
  content: "input",
  button: {
    text: "Search!",
    closeModal: false,
  },
})
.then(name => {
  if (!name) throw null;

  return fetch(`https://itunes.apple.com/search?term=${name}&entity=movie`);
})
.then(results => {
  return results.json();
})
.then(json => {
  const movie = json.results[0];

  if (!movie) {
    return swal("No movie was found!");
  }

  const name = movie.trackName;
  const imageURL = movie.artworkUrl100;

  swal({
    title: "Top result:",
    text: name,
    icon: imageURL,
  });
})
.catch(err => {
  if (err) {
    swal("Oh noes!", "The AJAX request failed!", "error");
  } else {
    swal.stopLoading();
    swal.close();
  }
});

我正在嘗試使用 nodejs 進行fetch

為此,我改變了行

return fetch(`https://itunes.apple.com/search?term=${name}&entity=movie`);

進入

return fetch('http://localhost:3000/search?movie_name=' + name, {
                method: 'GET'
            });

(我將應用程序設置為偵聽端口 3000)。

在我的 nodejs 文件中,我添加了一個 GET 路由:


app.get('/search', async function(req, resp) {
    try {
        let name = req.query.name;
        let response = await fetch('https://itunes.apple.com/search?term=' + name + '&entity=movie');
        let body = await response.text();
        let json = JSON.parse(body);
        resp.status(200).send(json);
    } catch (error) {
        resp.status(500).send();
    }
});

問題是,無論我輸入什么作為輸入,我都會得到相同的 JSON 文件。

我對nodejs很陌生,感謝所有幫助!

JSON 文件,我不斷收到任何輸入(例如http://localhost:3000/search?movie_name=bob ):

{"resultCount":2,"results":[{"wrapperType":"track","kind":"feature-movie","trackId":1469900435,"artistName":"Barak Goodman","trackName":"Woodstock: Three Days that Defined a Generation","trackCensoredName":"Woodstock: Three Days that Defined a Generation","trackViewUrl":"https://itunes.apple.com/us/movie/woodstock-three-days-that-defined-a-generation/id1469900435?uo=4","previewUrl":"https://video-ssl.itunes.apple.com/itunes-assets/Video113/v4/23/21/ff/2321ffa0-9389-63f8-1521-59de6b73ac87/mzvf_6812907527133973755.640x480.h264lc.U.p.m4v","artworkUrl30":"https://is3-ssl.mzstatic.com/image/thumb/Video123/v4/b5/cc/e1/b5cce1ab-e415-7084-c9e9-9dfcf512374c/source/30x30bb.jpg","artworkUrl60":"https://is3-ssl.mzstatic.com/image/thumb/Video123/v4/b5/cc/e1/b5cce1ab-e415-7084-c9e9-9dfcf512374c/source/60x60bb.jpg","artworkUrl100":"https://is3-ssl.mzstatic.com/image/thumb/Video123/v4/b5/cc/e1/b5cce1ab-e415-7084-c9e9-9dfcf512374c/source/100x100bb.jpg","collectionPrice":4.99,"trackPrice":4.99,"trackRentalPrice":4.99,"collectionHdPrice":5.99,"trackHdPrice":5.99,"trackHdRentalPrice":4.99,"releaseDate":"2019-08-06T07:00:00Z","collectionExplicitness":"notExplicit","trackExplicitness":"notExplicit","trackTimeMillis":5845247,"country":"USA","currency":"USD","primaryGenreName":"Documentary","contentAdvisoryRating":"Unrated","shortDescription":"Celebrate the 50th anniversary of the concert that became a touchstone for a generation. This film","longDescription":"Celebrate the 50th anniversary of the concert that became a touchstone for a generation. This film brings the concert to life through the voices of those who were present at what became the defining moment of the counterculture revolution."},{"wrapperType":"track","kind":"feature-movie","trackId":648772372,"artistName":"Laura Archibald","trackName":"Greenwich Village: Music that Defined a Generation","trackCensoredName":"Greenwich Village: Music that Defined a Generation","trackViewUrl":"https://itunes.apple.com/us/movie/greenwich-village-music-that-defined-a-generation/id648772372?uo=4","previewUrl":"https://video-ssl.itunes.apple.com/itunes-assets/Video118/v4/1d/fc/ce/1dfcce43-f789-7baf-48d9-998b3b264692/mzvf_2471105163605910750.640x480.h264lc.U.p.m4v","artworkUrl30":"https://is4-ssl.mzstatic.com/image/thumb/Video2/v4/fc/3b/87/fc3b8703-8069-6646-f99e-fcfa8bed70c8/source/30x30bb.jpg","artworkUrl60":"https://is4-ssl.mzstatic.com/image/thumb/Video2/v4/fc/3b/87/fc3b8703-8069-6646-f99e-fcfa8bed70c8/source/60x60bb.jpg","artworkUrl100":"https://is4-ssl.mzstatic.com/image/thumb/Video2/v4/fc/3b/87/fc3b8703-8069-6646-f99e-fcfa8bed70c8/source/100x100bb.jpg","collectionPrice":9.99,"trackPrice":9.99,"trackRentalPrice":4.99,"collectionHdPrice":12.99,"trackHdPrice":12.99,"trackHdRentalPrice":4.99,"releaseDate":"2013-06-18T07:00:00Z","collectionExplicitness":"notExplicit","trackExplicitness":"notExplicit","trackTimeMillis":5541875,"country":"USA","currency":"USD","primaryGenreName":"Documentary","contentAdvisoryRating":"Unrated","shortDescription":"An all-star cast of characters including Pete Seeger, Carly Simon, Richie Havens and Susan Sarandon","longDescription":"An all-star cast of characters including Pete Seeger, Carly Simon, Richie Havens and Susan Sarandon came together in ‘60s Greenwich Village creating a social, cultural and political vortex through their desire to make change. Their stands against social and racial injustice through words and music went beyond their celebrity to create an everlasting effect on generations to come.  A FilmBuff Presentation."}]}

請注意:

sweetAlert 是 JavaScript 的 window.alert() function 的替代品,它顯示了非常漂亮的模態 windows。 它是一個沒有依賴關系的獨立庫,它由 JavaScript 文件和 CSS 文件組成。

定義

似乎錯字:而不是用name調用查詢,而是用movie_name調用。 req.query.name; 在快遞 js 中。

瀏覽器

return fetch('http://localhost:3000/search?movie_name=' + name, {
                method: 'GET'
            });

快遞:

app.get('/search', async function(req, resp) {
    try {
        let name = req.query.name;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM