Node.js සිංහලෙන් ඉගෙන ගමු - පාඩම් මාලාව 2



අපි බලමු කොහොමද basic server එකක් හදා ගන්නේ කියල. Node.js වල තිබෙන http module එක භාවිතා කරල තමයි අපි server එක config කරගන්නෙ. ඒ සඳහා මුලින්ම අලුත් server.js file එකක් open කරගන්න. දැන් අපි කරන්න ඕන http module එක අපේ file එකේ භාවිතා කරන්න පුලුවන් විදියට import කරගන්න එක. ඒ සඳහා මුලින්ම,

var http = require('http');

ලෙස http module එක include කරගන්න. දැන් පහත code එක එකතු කරන්න.

var server = http.createServer(function (request, response) {
          response.end("Hello");
});


මෙතැනදී වෙන්නෙ createServer() method එකන් server එකක් create වෙන එක. මේ method එකේ argument එකක් විදියට callback function එකක් තමයි දීල තිබෙන්නෙ. මෙතැනදී වෙන්නෙ අපි හදාගත් server එකට එන හැම request එකකදීම මේ callback function execute වෙනවා. ඉතින් මේ function එක ඇතුලෙ තමයි අපේ server එක කරන හැම දෙයක්ම ඇතුලත් වෙන්න ඕන. callback function එකේ arugument දෙක තමයි request, response. request object එකේ client ගේ request එකට අදාළ සියලුම විස්තර අන්තර් ගත වෙනෙව. උදාහරණයක් විදියට client ගේ IP එක මොකක්ද, request method එක මොකක්ද (GET, POST, etc), request URL එක මොකක්ද වගේ දේවල් මේ request object එකේ ඇතුලත් වෙලා තිබෙනව. ඒ වගේම response object එකේ server එකෙන් clientට යවන response එක ගැන විතර තමයි ඇතුලත් වෙලා තිබෙන්නෙ. උදාහරණයක් විදියට response header එකේ ඇතුලත් වෙන්නෙ මොනවද, යවන data මොනවද වගේ දේවල් response object එකේ තමයි ඇතුලත් වෙන්නෙ.
අවසානයේ පහත code එක ඇතුලත් කරන්න.

server.listen(3000, function(err) {
          console.log("Server is running on localhost:3000")
});


මෙතැනදී වෙන්නෙ අපි create කරගත් server එක 3000 port එකේ run කරපු එක. server එකක් භාවිතා කරන්නනම් අනිවාර්යයෙන්ම පොර්ට් එකකට assign කරල තිබෙන්න ඕන. දැන් අපි server.js file එක තිබෙන ස්ථානයේ console එක open කර node server ලෙස type කර අපේ server එක run කර ගන්න. server එක run උනාම console එකේ Server is running on localhost:3000 ලෙස පෙන්වයි. දැන් අපිට browser එකට ගිහින් http://localhost:3000 address එකට ගියවිට Hello ලෙස සටහන් වී තිබෙනවා දැකගන්න පුළුවන්.

අපි තවදුරටත් request & response objects භාවිතා කරන ආකාරය බලමු.

1. request

මුලින්ම client request කරන URL retrieve කරගන්නෙ කොහොමද බලමු.
අපිට පුළුවන් ඒ සඳහා request.url භාවිතා කරන්න. මුලින්ම server.js එකේ createServer() method එක පහත පරිදි වෙනස් කරන්න.

var server = http.createServer(function (request, response) {
          console.log(request.url);
          response.end("Hello");
});


දැන් පෙර පරිදිම server එක run කර browser එකන් localhost:3000 address එකට යන්න. එවිට console එකේ / ලෙස සටහන් වෙයි. දැන් localhost:3000/index address එකට යන්න. එවිට console එකේ /index ලෙස සටහන් වෙයි. මෙන්න මේ විදියට client request කරන URL එක මොකක්ද කියන එක අපිට බලාගන්න පුළුවන්. ඒ අනුව අදාල URL එකට අදාල data, response එක විදියට යවන්න පුළුවන්. උදාහරණයක් ලෙස,

var server = http.createServer(function (request, response) {
          console.log(request.url);
          if(request.url === "/index") {
                    response.end("You requested index");
          } else if(request.url === "/profile") {
                    response.end("You requested profile");
          } else {
                    response.end("Anyway, Hello");
          }
});


දැන් අපි බලමු client request එකට අදාල method එක retrieve කරන්නෙ කොහොමද කියල. මේකට අපිට request.method භාවිතා කරන්න පුළුවන්. මුලින්ම අපේ server.js එක පහත ආකාරයට වෙනස් කරන්න.

var server = http.createServer(function (request, response) {
    console.log(request.method);
    response.end("Hello");
});


දැන් server එක run කර පෙර පරිදිම browser එකන් address එකට පිවිසෙන්න. එවිට console එකේ GET ලෙස දිස් වෙයි. එනම් අපි server එකට GET request එකක් යැවූ බව පෙන්වයි. වෙනත් method වලින් request යැවීමටනම් browser එකේ REST client එකක් install කල යුතුය. Chrome වලදිනම් Postman, Firefox වලදිනම් RESTED වගේ client එකක් භාවිතා කරල වෙනත් method වලින් request යවා බලන්න.


මේ ආකාරයට clientගේ IP එක retrieve කිරීමට request.connection.remoteAddress භාවිතා කරන්න පුළුවන්.

2. response

මුලින්ම බලමු කොහොමද response එකක Status Code එක වෙනස් කරන්නේ කියල. Status Code එකන් තමයි client request එක අදාල response එක success ද නැත්ද තීරණය කරන්නෙ. Status Code ගැන වැඩිදුරටත් කියවා බලන්න. අපිට response.statusCode භාවිතා කර අදාල code එක response object එකට assign කරන්න පුලුවන්. උදහනයක් විදියට client request කරන URL එක server එකට හදුනාගන්න බැරිනම් 404 Not Found status එක අපිට response එක විදියට යවන්න පුලුවන්. ඒ සදහා server.js එකේ code එක පහත පරිදි වෙනස් කරන්න.

var server = http.createServer(function (request, response) {
          if(request.url === "/home") {

                     response.statusCode = 200;
                     response.end("Home");
           } else {
                     response.statusCode = 404;
                     response.end("Not Found");
           }
});


අපිට response එකේ සියලු විස්තර බලාගැනීමට browser එකේ developer option වල Network option එක අදාරයෙන් පුලුවන්.

  

response header එක වෙනස් කිරීමට response.writeHead භාවිතා කරන්න පුළුවන්. මෙහිදී statusCode එක වගේම contentType එක එහෙමත් නැත්නම් අපේ response එක HTML page එකක්ද JSON එකක්ද වැනි details client ට send කරන එක කරන්න පුළුවන්. උදාහරණයක් විදියට,

var server = http.createServer(function (request, response) {
          response.writeHead(200, {"ContentType": "text/html"});
          response.end(
                    "<html>"
                    + "<body>Hello</body>"
                    + "</html>"
          );
});


ඒ වගේම response.write() method, response එක end නොකර data append කරන්න භාවිතා කරන්න පුළුවන්. නමුත් අවසානයේ response එක end කිරීම අනිවාර්ය වෙනවා.

ඉදිරි පාඩමෙන් බලමු GET request POST request handle කරන්නෙ කොහොමද කියල.

Comments

Popular posts from this blog