Node.js examples from Cook's book ⬆
Directory samples_Cook\ contains Node.js examples presented in Fabian Cook's book "Node.js Essentials" (Packt, 2015). |
🔎 The numbering used below – eg.
03
in example03_basic_auth
– refers to the chapter where the example belongs to.
Command npm.cmd start
starts the server application app\app.js
which listen to our requests on port 8180
(defined in file config.json
1):
> cd N:\samples_Cook\03_basic_auth > start "basic_auth" npm start > basic_auth@1.0.0 start N:\samples_Cook\03_basic_auth > node app/app.js [2018-05-10 19:43:17 INFO] (app.js) Listening on port 8180 [2018-05-10 19:43:33 INFO] (app.js) Requested URL: / [2018-05-10 19:43:33 INFO] (app.js) Requested URL: / [2018-05-10 19:43:33 INFO] (app.js) Requested URL: /
Command npm.cmd run client
sends several requests to the server:
> npm run client > basic_auth@1.0.0 client N:\samples_Cook\03_basic_auth > node npm_scripts/start_client.js en / fr / de /
03_bearer_token
Example ▴
Command npm.cmd start
starts the server application app\app.js
which listen to our requests on port 8180
(defined in file config.json
1):
> start "bear_token" npm start > bearer_token@1.0.0 start N:\samples_Cook\03_bearer_token > node . [2020-07-14 19:06:21 INFO] (app.js) Listening on port 8180
Command npm.cmd run client
sends a POST
request to the server (endpoint /login
):
> npm run client > bearer_token@1.0.0 client N:\samples_Cook\03_bearer_token > node npm_scripts/start_client.js [2020-07-14 19:08:02 INFO] (start_client.js) login: username=foo [2020-07-14 19:08:02 INFO] (start_client.js) curl -H "User-Agent: Mozilla/5.0" -H "Content-Type: application/json" -X POST -d "{\"username\": \"foo\", \"password\":\"bar\"}" http://127.0.0.1:8180/login token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJmb28iLCJpYXQiOjE1OTQ3NDY0ODJ9.xCpqkBxRTZ-JfC_HpB15GKur4tjJNuHEWFqhkXkCqtM
Command npm start
starts the server application app\app.js
which listen to our requests on port 8180
(defined in file config.json
1):
> start "bear_token2" npm start > bearer_token2@1.0.0 start N:\samples_Cook\03_bearer_token2 > node . [2021-06-02 12:34:21 INFO] (app.js) Listening on port 8180
Command npm run client
sends a POST
request to the server (endpoint /login
):
> npm run client > bearer_token@1.0.0 client N:\samples_Cook\03_bearer_token2 > node npm_scripts/start_client.js [2021-06-02 12:36:18 INFO] (start_client.js) login: username=foo [2021-06-02 12:36:18 INFO] (start_client.js) curl -H "User-Agent: Mozilla/5.0" -H "Content-Type: application/json" -X POST -d "{\"username\": \"foo\", \"password\":\"bar\"}" http://127.0.0.1:8180/login token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJmb28iLCJpYXQiOjE2MjI2MzAxNzl9.-vkWn3KQVZdvZh4Eboe1AA7g3vINa7g71c-_uZcQ1j0 [2021-06-02 12:36:19 INFO] (start_client.js) curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJmb28iLCJpYXQiOjE2MjI2MzAxNzl9.-vkWn3KQVZdvZh4Eboe1AA7g3vINa7g71c-_uZcQ1j0" -X GET http://127.0.0.1:8180/userinfo [2021-06-02 12:36:19 INFO] (start_client.js) json={"id":1,"username":"foo"}
Command npm.cmd start
starts the server application app\app.js
which listen to our requests on port 8180
(defined in file config.json
1):
> start "logging_morgan" npm start > logging_morgan@1.0.0 start N:\samples_Cook\04_logging_morgan > node . Server running on port 8180 GET /info 200 - - 1.651 ms
Command npm.cmd run client
sends a GE
request to the server (endpoint /info
):
> npm run client > bearer_token@1.0.0 client N:\samples_Cook\03_bearer_token2 > node npm_scripts/start_client.js [2022-05-27 09:54:46 INFO] (start_client.js) curl -H "User-Agent: Mozilla/5.0" -X GET http://127.0.0.1:8180/info [2022-05-27 09:54:46 INFO] (start_client.js) json={"node":"14.20.0","v8":"8.4.371.23-node.87","uv":"1.42.0","zlib":"1.2.11","brotli":"1.0.9","ares":"1.18.1","modules":"83","nghttp2":"1.42.0","napi":"8","llhttp":"2.1.4","openssl":"1.1.1o","cldr":"40.0","icu":"70.1","tz":"2021a3","unicode":"14.0"}
Footnotes ▴
[1] config.json
↩
-
Only the template file
config_TEMPLATE.json
is stored in our GitHub repository. The user has to copy it toconfig.json
and update the two JSON fieldshost
andport
as desired before running the above code examples.