Professional Documents
Culture Documents
Giáo Trình Lập Trình Web Với Node Và Express
Giáo Trình Lập Trình Web Với Node Và Express
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');