Otworzyłem terminal w katalogu take-a-rest i uzyłem komendy npm install. Powstał nowy plik
package-lock.json
Etap 1:
Użyłem komendy npm run 01_HttpServer by uruchomić serwer
W drugim terminalu połączyłem się za pomocą http (bez s) z localhostem 3000
Kod aplikacji odpowiedzialny za komunikaty:By dodać informację o czasie do odpowiedzi, zmodyfikowałem kod:
Dla localhost:3000/hello:
//response.send("<p>Anonymous message: Oh, Hi Mark!</p>");
response.send(`<p>Anonymous message: Oh, Hi Mark! The datetime is:
${Date.now()}</p>`);
Efekt:
Dla strony głównej:response.send(`<h1>HTTP Server</h1><p>Go to /hello subpage! The datetime is:
${Date.now()}</p>`);
//response.send("<h1>HTTP Server</h1><p>Go to /hello subpage!</p>");
Efekt:
Dodałem obsługę zapytania GET dla /time:
app.get("/time", function(request, response) {
printReqSummary(request);
response.send(`<p>The datetime is: ${Date.now()}</p>`);
});
Wynik:
Zamknąłem serwer 01_HttpServer i uruchomiłem serwer 02_UrlParameters
Sprawdziłem działanie:Zwracanie losowego argumentu:
Do pliku app.js dodałem poniższy kod:
function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min;
}
// GET /X/Y/Z - return 1 of parameters
app.get("/:x/:y/:z", function(request, response) {
printReqSummary(request);
const x = request.params.x;
const y = request.params.y;
const z = request.params.z;
const rand = getRandomInt(0, 2);
out = 0;
if( rand == 0 )
out = x
if( rand == 1 )
out = y
if( rand == 2 )
out = z
response.send(`<p>Random input value is: ${out}</p>`);
});
Etap 3:
Zamknąłem serwer z etapu 2. i uruchomiłem serwer 03_HttpMethods
Sprawdzenie działania:
Zmodyfikowałem plik app.js by dodawanie itemów było realizowane za pomocą POST a nie
PUT
/* PUT /item/:name -- add (put) new item to the collection */
//app.put("/item/:name", function(request, response) {
app.post("/item/:name", function(request, response) {
printReqSummary(request);
const itemName = request.params.name;
/* Is the item in collection? */
if (items.includes(itemName)) {
response.send(`<p>Item "${itemName}" already in collection</p>`);
} else {
items.push(itemName);
response.send(`<p>Item "${itemName}" added successfully</p>`);
}
});
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}