In the first lesson, curl showed you the raw exchange for Portugal’s 2026 holidays: a status line reading HTTP/1.1 200 OK, a Content-Type header, and a body of JSON text. The planner can’t run curl for every friend, though. It has to send the same request from JavaScript and read the same parts of the answer.
You may have fetched data in a script before with two await expressions and logged what came back. In this lesson, you’ll send the planner’s first real request that way, then read the response’s status code, its ok flag, and its headers, and match each one to a line curl printed. You’ll also load the saved file from the first lesson when the server doesn’t answer.
Send the Planner’s First Request
curl printed Portugal’s holidays on your screen, but the planner needs them inside a running program. The function that sends a request to a server from JavaScript is fetch(), which is built into Node.js and every browser. You give it a URL, and it returns a promise. Save this as holidays.mjs in your planner folder:
holidays.mjs
const url =
"https://date.nager.at/api/v4/Holidays/PT/2026";
const response = await fetch(url);
const holidays = await response.json();
console.log(holidays.length, "holidays");
console.log(holidays[0].date, holidays[0].name);
The .mjs extension makes the file an ES module, so await works at its top level. Run it, and Nager.Date answers:
$ node holidays.mjs
17 holidays
2026-01-01 New Year's Day
This output is from a run on November 27, 2026, and it changes only if Nager.Date updates Portugal’s list. Here’s the two-await pattern again, with this course’s request:
- The first
awaitwaits forfetch(url). With only a URL,fetch()sends aGETrequest, as curl did. The promise fulfills with an object that represents the server’s answer, called aResponse. - The second
awaitwaits forresponse.json(). That method reads the body, which is still text, and parses it as JSON, the same jobJSON.parse()did in the REPL in the first lesson. Reading the body takes time too, soresponse.json()returns another promise.
Only then is holidays an array you can index. Notice also that the URL is complete, origin included. In a browser, fetch() can take a bare path and resolve it against the page’s address. A Node.js script has no page, so with a path such as "/api/v4/Holidays/PT/2026", fetch() still returns a promise, but that promise rejects with a TypeError whose message starts Failed to parse URL. The first await is where the error is thrown.