19
To begin, let’s add a dependency to our project. We’re going to use the not-very-
creatively-but-very-accurately-named
request module, which will provide to our server
an elementary HTTP client for it to use to make remote calls:
npm
install request --save
With that done, copy that server.js file and name it server_time.js, then replace
its contents with the following code:
require("http").createServer((inRequest, inResponse) => {
const requestModule = require("request");
requestModule(
"http://worldtimeapi.org/api/timezone/America/New_York",
function (inErr, inResp, inBody) {
inResponse.end(
`Hello from my first Node Web server: ${inBody}`
);
}
);
}).listen(80);
As you probably recognize, it’s
the same code as before, but now with a bit more
inside the callback function provided to createServer(). What we’re doing now is
firstly to import the
request module and to give it the name requestModule (just to help
disambiguate it from the inRequest object passed into the callback function).
The API
for this module is straightforward: pass a URL to the constructor, plus a callback, and
a call will be made to the URL, and the provided callback
will be executed when the
response to that call comes back. The URL here is to the World Time API, which you
can read about here:
http://worldtimeapi.org
. This particular form of the URL (the
API provides a few) takes in a time zone, America/New_York here (though you should
certainly feel free to replace that with a time zone you prefer –
you can access
http://
worldtimeapi.org/api/timezone
in a browser to see a list of available time zones).
What we get back is a chunk of JSON, which is then written to the response that is
returned to the browser.
Chapter 1 Server-Side aCtion: node and npM
20
The final step is you’ll need to edit package.json to
change that start value to
indicate the new JavaScript file, after which you can launch the app and try it. The
response you’ll see now isn’t necessarily pretty:
Hello from my first Node Web server: {"week_number":36,"utc_offset":
"-04:00","utc_datetime":"2019-09-06T17:22:45.406437+00:00","unixtime":
1567790565,"timezone":"America/New_York","raw_offset":-18000,"dst_until":
"2019-11-03T06:00:00+00:00","dst_offset":3600,"dst_from":"2019- 03- 10T07:
00:00+00:00","dst":true,"day_of_year":249,"day_of_week":5,"datetime":
"2019-09-06T13:22:45.406437-04:00","client_ip":"12.198.42.69",
"abbreviation":"EDT"}
…but it gets the job done (and, if like me, you have a browser extension that
automatically “prettifies” JSON, then what you’ll see will be, well,
prettier!).
This is a good example not only of adding dependencies
to a project and using
them but also of the asynchronous nature of Node I talked about earlier. Here, the
call to the World Time API takes some time, and the response to the request coming
from the browser is queued up and awaits that response before executing the callback
passed to the requestModule() constructor, which then produces
the final response to
the browser. But, all that time, Node was free to handle other incoming requests; work
wasn’t held up awaiting the remote request, that’s the key thing.
Do'stlaringiz bilan baham: