简体   繁体   中英

nodejs fetch API returning the same JSON file

I am trying to learn nodejs by building my own rest API.

I have copied this code from 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();
  }
});

and I am trying to do the fetch using nodejs.

To do this, I changed the line

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

into

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

(I set app to listen on port 3000).

and in my nodejs file, I added a GET route:


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();
    }
});

The problem is, no matter what I type in as the input, I get the same JSON file back.

I am quite new to nodejs and appreciate all the help!

JSON file that I keep receiving with any input (eg 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."}]}

Please note:

sweetAlert is a replacement for JavaScript's window.alert() function that shows very pretty modal windows. It's a standalone library that has no dependencies, and it's made from a JavaScript file plus a CSS file.

definition

Seems typo: instead of calling query with name you calling with movie_name . Which does not match with req.query.name; in express js.

Browser

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

Expressjs:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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