Theory
server જે ક્યારેય listen નથી કરતું
તમારા 5-line server માં એક social flaw છે: તે દરેકને same thing કહે છે. schedule માંગો, seat count, અથવા day 2 ના events: same greeting.
સાચો web server answer આપતા પહેલાં question ને READ કરે છે. અને question, તમે already Unit 4 ના GET lessons થી જાણો છો, URL માં travel કરે છે:
localhost:8080/?event=garba&day=2
? પછીનું બધું એ query string છે, અને today server finally split અને answer કરવાનું શીખે છે.
Theory
writeHead: body પહેલાં label
res.writeHead(200, { 'Content-Type': 'text/html' });
પ્રથમ વસ્તુ જે response declare કરે છે: status code (200 OK, 404 Not Found: very codes જે તમારા AJAX guard ને check કર્યા!) અને headers, chiefly Content-Type:
text/html: browser જે follow થાય છે તેને page તરીકે renders કરે છેapplication/json: answer એ API data છે: exactly શું AJAX caller ના JSON.parse ને expect છે
Order law: writeHead કોઈ પણ write અથવા end પહેલાં આવે છે: તમે envelope ને seal થયા પછી label નથી કરી શકતા.
Theory
query string ને Splitting કરવું
/?event=garba&day=2 ની visit તમારા callback માં lands થાય છે req.url સાથે exactly તે text ને hold કરતું. તમે તેને hand થી split કરી શકો છો (ચોપ ?, split on &, split on =), પણ Node નું built-in url module તેને properly કરે છે:
const q = url.parse(req.url, true).query;
url.parse(req.url, true)URL ને dissects કરે છે; true query ને parsed OBJECT તરીકે માંગે છે- તેની
.queryproperty પછી{ event: 'garba', day: '2' }છે
quotes નો note '2' પર: query values strings તરીકે arrive થાય છે, same convert-before-maths rule જેમ દરેક input અત્યાર સુધી.
Practical
server જે question ને answer કરે છે
const http = require('http');
const url = require('url');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
const q = url.parse(req.url, true).query; // query string ને split કરો
if (q.event == "garba") {
res.write('<h1>Garba Night</h1>');
res.write('<p>Day ' + q.day + ', Main Ground, 40 seats left</p>');
} else {
res.write('<p>Ask me with ?event=garba&day=2</p>');
}
res.end();
}).listen(8080);
// Visit: localhost:8080/?event=garba&day=2
// Answer: Garba Night, Day 2, Main Ground, 40 seats left
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
visitor localhost:8080/?event=garba&day=2 ને open કરે છે. શું url.parse(req.url, true).query return કરે છે?
- string "?event=garba&day=2", unchanged
- object { event: 'garba', day: '2' }, values strings તરીકે
- object { event: 'garba', day: 2 }, day number તરીકે
- array: ['garba', '2']
Show the answer
object { event: 'garba', day: '2' }, values strings તરીકે
બીજા argument તરીકે true સાથે, parse query ને ready object માં split કરે છે જે parameter names દ્વારા keyed છે: q.event અને q.day just work. subtlety જે option C miss કરે છે: values ALWAYS strings છે: '2', 2 નહીં: URLs text ને carry કરે છે, તેથી q.day પર arithmetic ને parseInt ની જરૂર છે first, same lesson જે TextBoxes અને InputBoxes શીખવ્યા. Option A એ શું req.url તમને આપે છે BEFORE parsing (well, / prefix સાથે). Option D names ને lose કરે છે, જે whole point છે: parameters name દ્વારા answer આપે છે, position દ્વારા નહીં.
Think first
full-stack loop ને Close કરો
Unit 4 ના live search એ xhr.open('GET', 'search.php?q=g') ને send કર્યું. server side ને તમારા head માં rewrite કરો: શું આ Node server એ listing થી differently કરવું જોઈએ તે AJAX caller ને serve કરવા માટે: think Content-Type અને response format, પછી tap.
Show the answer
બે changes: writeHead application/json ને send કરે છે (caller JSON.parse કરશે, render નહીં), અને body JSON string બને છે: events ને filter કરો જેના names q.q ને contain કરે છે, પછી res.end(JSON.stringify(matches)). browser side જે તમે Unit 4 માં wrote તેને ZERO changes ની જરૂર છે: તેનો responseText હવે તમારા server થી arrive થાય છે. એ full stack છે એક language માં: AJAX engine asks કરે છે, event loop answers કરે છે, JSON in between: અને તમે personally તે sentence નો દરેક piece લખ્યો છે.
Watch out
Web-server slips
write after writeHead: headers body ને follow નથી કરી શકતા: ERR_HTTP_HEADERS_SENT: label first, હંમેશા.
query values પર Maths: q.day + 1 એ '21' territory છે: parseInt(q.day) first.
query ને blindly Trusting કરવું: q.event missing (undefined) અથવા nonsense હોઈ શકે છે: defensively branch કરો, જેમ listing નું else કરે છે. દરેક visitor-supplied value એ guest છે, family નહીં.
Theory
subject માં એક lesson બાકી
server હવે listen, split અને answer કરે છે: પણ તે હજુ કંઈ remember નથી કરતું: દરેક restart બધા registrations ને forget કરે છે. finale Node ને memory આપે છે: fs module: reading, creating, updating, deleting અને renaming FILES: registrations છેલ્લે disk પર લખાય છે, અને FestConnect Live complete XML થી persistent server સુધી.
Summary
Key takeaways
- writeHead(status, headers) response ને labels કરે છે કોઈ પણ body પહેલાં: 200/404, અને Content-Type text/html vs application/json.
- visitor નો query string req.url પર ride કરે છે: /?event=garba&day=2.
- url.parse(req.url, true).query તેને ready object માં split કરે છે: q.event, q.day.
- Query values strings છે: arithmetic પહેલાં parseInt.
- personally answer કરવા query પર branch કરો; missing parameters સામે guard કરો.
- Content-Type ને application/json + JSON.stringify માં swap કરો, અને આ server Unit 4 ના AJAX ને unchanged answer કરે છે.
- Memory hook: envelope ને label કરો, question ને split કરો, name દ્વારા answer આપો.