You are on page 1of 5

Gio Trnh Lp Trnh Web vi Node v Express

Dch da theo cun Web Development with Node and


Express
Cc yu cu v kin thc c bn cn nm vng trc khi bc vo hc:
1.
2.
3.
4.

Hiu r cc th tag trong HTML


Bit s dng tng i tt ngn ng Javascript
C kt ni mng
C kh nng t tm hiu, nghin cu tt

Chng 1: Gii thiu v Express


-

u tin ni v Node hay Nodejs:


o Node l mt ngn ng da trn javascript nhng c iu n l mt
ngn ng back-end ch khng phi front-end nh javascript. iu
c ngha node cng tng t nh cc ngn ng lp trnh web khc nh
php, asp.net, python hay ruby.
o iu ng ch Node l n lm vic theo kiu khng ng b. Hin
ti c th bn s cha hiu nhng sm thi bn s bit n l ci g.
o Thng tin cc bn c th tm hiu a ch ny:
http://vi.wikipedia.org/wiki/Node.js
Express l g?
o Express l mt framework dnh cho ngn ng nodejs, cung cp cc
tnh nng mnh m cho vic xy dng mt ng dng web ng ngha
hoc lai.
o Express cng c th s dng xy dng mt API mnh m v thn
thin vi ngi dng, v n cung cp rt nhiu tin ch HTTP v
middleware cho vic kt ni.
o Express cung cp cc tnh nng cho bt k ng dng web no, nhng
khng lm mt nhng g khin bn yu thch Node.JS
o Thng tin thm cc bn c th tham kho ti:
http://en.wikipedia.org/wiki/Express.js
hoc trang ch: http://expressjs.com/

khng mt thi gian mnh s khng ni di dng v tiu s, lch s, cng


nh thng tin khng qu quan trng v ngn ng. Cc bn mun tm hiu thm c
th dng google tra cu. :D
Chng 2: Bt u vi Node
B1. Chun b
-

iu u tin khi bn mun bt u hc nodejs th bn hy vo trang ch ca


nodejs v ti bn nodejs mi nht v: http://nodejs.org/. Ch hy la chn
ng phin bn h iu hnh ph hp.
V ci t chc cc bn khng cn phi gii thiu. Sau khi ti xong vic bn
cn l next v next.

Khi bn hc nodejs bn phi lm quen vi nhng cu lnh bng tay. V t c


IDE no h tr. Hy lm quen vi notepad++ vit cu lnh v ca s cmd
chy lnh.
Tip theo bn s phi lm quen vi npm(node package manager): Hy m
cmd ln v th g cu lnh: npm install g grunt-cli
Nu cmd khng sinh ra li th bn c th sn sng ti bc tip theo.

B2. To mt Web server n gin vi Node


Bt u vi chng trnh Hello World.
Bn hy m notepad++ v dn on code ny v lu li di tn file helloworld.js
v hy nh ni lu
li file ca bn.
var http = require('http');
http.createServer(function(req,res){
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello world!');
}).listen(3000);
console.log('Server started on localhost:3000; press Ctrl-C to terminate....');
M th mc cha file helloworld.js chn vo khong trng v nhn gi phm shift +
chut phi -> open command window here .
Sau g lnh: node helloworld.js
Tip theo bn thy trn ca s command line hin dng ch: Server started on
localhost:3000; press Ctrl-C to terminate....
OK Hy m trnh duyt ra v vo a ch: http://localhost:3000/
v kim tra kt qu.
B3. nh Tuyn( Routing)
nh tuyn l cch m lp trnh vin iu hng ngi dng khi c yu cu ca
ngi dng di dng URL. Hy lm quen vi n vi v d di y.
Gi chng ta hy to th mt b nh tuyn ngi dng da trn nodejs. Thay th
ni dung file helloworld.js bng on m di y.
var http = require('http');
http.createServer(function(req,res){
// xa cc truy vn,du gch cho v chuyn v ch thng
var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
switch(path) {

case '':
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Homepage');
break;
case '/about':
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('About');
break;
default:
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
break;
}
}).listen(3000);
console.log('Server started on localhost:3000; press Ctrl-C to terminate....');
M ca s cmd trong th mc cha file helloword.js v chy lnh tng t nh v d
trc.
node helloworld.js
M trnh duyt v truy cp li a ch: http://localhost:3000/
Gi y bn c th truy cp thm vo a ch http://localhost:3000/about
hoc a ch khc ph hp trong cu lnh switch-case. Bn hon ton c th to cc
router khc da trn form trn. Hy lm th v d to ng dn
http://localhost:3000/address cha a ch nh, s in thoi, thng tin v h tn
ca bn.
B4. Cung cp ti nguyn tnh.
Trong trng hp trang web ca bn tng i ln hoc nhiu trang. Bn khng
mun phi vit ht ra cng mt file nh vy v rt kh qun l chng cng nh
debug. Vy chng ta hy tm hiu mt cch khc qun l b code trn da trn
vic cung cp cc ti nguyn tnh trn server.
Chnh sa b code trong file helloworld.js li thnh :
var http = require('http'),
fs = require('fs');

function serveStaticFile(res, path, contentType, responseCode) {


if(!responseCode) responseCode = 200;
fs.readFile(__dirname + path, function(err,data) {
if(err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('500 - Internal Error');
} else {
res.writeHead(responseCode,{ 'Content-Type': contentType });
res.end(data);
}
});
}
http.createServer(function(req,res){
var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
switch(path) {
case '':
serveStaticFile(res, '/public/home.html', 'text/html');
break;
case '/about':
serveStaticFile(res, '/public/about.html', 'text/html');
break;
case '/img/logo.jpg':
serveStaticFile(res, '/public/img/logo.jpg', 'image/jpeg');
break;
default:
serveStaticFile(res, '/public/404.html', 'text/html',
404);
break;
}
}).listen(3000);

console.log('Server started on localhost:3000; press Ctrl-C to terminate....');


Hy m th mc cha file helloworld.js v to th mc public. M th mc public ra
v to nhng file home.html, about.html, 404.html vi ni dung nh di y:
<HTML>
HOME (Tn file)
Nhng g bn mun vit blah.blah.(Hy nh chnh li dng ny phn
bit cc file)
</HTML>
Sau m th mc public to 1 foler tn img . Tip theo tm 1 file nh ui jpg v
i tn n thnh logo.jpg .
Quay li th mc cha file helloworld.js v chy lnh: node helloworld.js
Tip tc m trnh duyt v th cc ng dn:
http://localhost:3000/about
http://localhost:3000/
http://localhost:3000/img/logo.jpg
Vy l bn lm quen vi Nodejs. Tip sau y chng ta s cng tm hiu v
framework Express. Hn cc bn chng sau.

You might also like