{"id":229,"date":"2018-05-14T00:04:00","date_gmt":"2018-05-13T17:04:00","guid":{"rendered":"http:\/\/thietkewebdalat.net\/blog\/?p=229"},"modified":"2018-05-14T00:04:00","modified_gmt":"2018-05-13T17:04:00","slug":"bai-1-chuong-trinh-dau-tien-node-js","status":"publish","type":"post","link":"https:\/\/thietkewebdalat.net\/blog\/bai-1-chuong-trinh-dau-tien-node-js\/","title":{"rendered":"B\u00e0i 1 &#8211; Ch\u01b0\u01a1ng tr\u00ecnh \u0111\u1ea7u ti\u00ean node js"},"content":{"rendered":"<p>Ch\u00e0o c\u00e1c b\u1ea1n ,h\u00f4m nay m\u00ecnh h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n vi\u1ebft ch\u01b0\u01a1ng tr\u00ecnh \u0111\u1ea7u ti\u00ean nh\u00e9:<\/p>\n<p>B\u01b0\u1edbc 1: B\u1ea1n t\u1ea1o m\u1ed9t th\u01b0 m\u1ee5c \u0111\u1ec3 l\u01b0u file code js nh\u00e9.<\/p>\n<p>B\u01b0\u1edbc 2: B\u1ea1n t\u1ea1o 1 file js v\u1edbi t\u00ean l\u00e0 &#8220;bai_1.js&#8221;<\/p>\n<p>B\u01b0\u1edbc 3: B\u1ea1n m\u1edf file &#8220;bai_1.js&#8221; b\u1eb1ng notepad++ n\u1ebfu b\u1ea1n ch\u01b0a c\u00f3 th\u00ec c\u00e0i v\u00e0o nh\u00e9.<\/p>\n<p>B\u01b0\u1edbc 4 : B\u1ea1n g\u00f5 n\u1ed9i d\u1ee5ng sau v\u00e0o ,nh\u1edb l\u00e0 g\u00f5 v\u00e0o nh\u00e9 \u0111\u1eebng n\u00ean copy:<\/p>\n<pre>\/\/khai b\u00e1o s\u1eed d\u1ee5ng module HTTP\r\nvar http = require('http');\r\nKh\u1edfi t\u1ea1o server ch\u1ea1y c\u1ed5ng 8000\r\nhttp.createServer(function (req, res) {\r\n   \/\/thi\u1ebft l\u1eadp gi\u00e1 tr\u1ecb server tr\u1ea3 v\u1ec1\r\n    res.writeHead(200, {'Content-Type': 'text\/html'});\r\n    res.end('Hello World!');\r\n}).listen(8080);\r\n<\/pre>\n<p>M\u00ecnh s\u1ebd gi\u1ea3i th\u00edch cho c\u00e1c b\u1ea1n hi\u1ec3u nh\u00e9<\/p>\n<p>Trong node js khi b\u1ea1n c\u1ea7n s\u1eed d\u1ee5ng m\u1ed9t module n\u00e0o th\u00ec b\u1ea1n require module \u0111\u00f3 v\u00e0o, \u1edf b\u00e0i n\u00e0y m\u00ecnh s\u1ebd n\u00f3i s\u01a1 qua cho c\u00e1c b\u1ea1n n\u1eafm v\u00e0 h\u00ecnh dung s\u01a1 l\u01b0\u1ee3c node js n\u00f3 nh\u01b0 th\u1ebf n\u00e0o qua b\u00e0i sau m\u00ecnh s\u1ebd n\u00f3i k\u1ef9 cho c\u00e1c b\u1ea1n hi\u1ec3u nh\u00e9.Nh\u01b0 v\u00ed d\u1ee5 \u1edf tr\u00ean l\u00e0 m\u00ecnh require module HTTP<\/p>\n<p>\u0110o\u1ea1n ti\u1ebfp theo l\u00e0 m\u00ecnh kh\u1edfi t\u1ea1o 1 c\u1ed5ng \u0111\u1ec3 ch\u1ea1y ,b\u1ea1n c\u00f3 th\u1ec3 kh\u1edfi t\u1ea1o c\u1ed5ng b\u1ea5t k\u1ef3 nh\u01b0ng tr\u1eeb nh\u1eefng c\u1ed5ng \u0111\u1eb7c bi\u1ec7t ra nh\u00e9 v\u00ed d\u1ee5 c\u1ed5ng 80.<\/p>\n<p>Ti\u1ebfp \u0111\u1ebfn\u00a0H\u00e0m\u00a0<code>res.writeHead<\/code>\u00a0d\u00f9ng \u0111\u1ec3 thi\u1ebft l\u1eadp c\u00e1c tham s\u1ed1 c\u1ee7a header khi g\u1eedi k\u1ebft qu\u1ea3 v\u1ec1 cho client.<\/p>\n<p>Cu\u1ed1i c\u00f9ng l\u00e0\u00a0res.end(&#8216;Hello World!&#8217;) c\u00e1i ch\u1ed7 n\u00e0y n\u00f3 s\u1ebd hi\u1ec7n th\u1ecb k\u1ebft qu\u1ea3 l\u00ean m\u00e0n h\u00ecnh,\u0111\u00e1ng l\u1ebb b\u1ea1n n\u00ean vi\u1ebft th\u1ebf n\u00e0y n\u00f3 s\u1ebd \u0111\u1ea7y \u0111\u1ee7 :<\/p>\n<p>res.write(<span class=\"hljs-string\">&#8216;Hello World!&#8217;<\/span>);<\/p>\n<p>res.end();<\/p>\n<p>C\u00f2n c\u00e1ch m\u00ecnh vi\u1ebft th\u00ec vi\u1ebft ng\u1eafn ng\u1ecdn h\u01a1n nh\u01b0ng k\u1ebft qu\u1ea3 t\u01b0\u01a1ng t\u1ef1 nhau<\/p>\n<p>Sau khi vi\u1ebft xong b\u1ea1n ch\u1ea1y nh\u01b0 sau :<\/p>\n<p>B\u01b0\u1edbc 1: M\u1edf CMD l\u00ean<\/p>\n<p>B\u01b0\u1edbc 2 : B\u1ea1n tr\u1ecf v\u1ec1 n\u01a1i b\u1ea1n l\u01b0u tr\u1eef file js<\/p>\n<p>B\u01b0\u1edbc 3 : B\u1ea1n ghi : &#8220;node bai_1.js&#8221;<\/p>\n<p>B\u01b0\u1edbc 4 : B\u1ea1n m\u1edf tr\u00ecnh duy\u1ec7t l\u00ean v\u00e0 g\u00f5 http:\/\/localhost:8080 b\u1ea1n s\u1ebd th\u1ea5y k\u1ebft qu\u1ea3 nh\u00e9.<\/p>\n<p>H\u00f4m nay t\u1edbi \u0111\u00e2y th\u00f4i ,H\u00f4m nay ch\u00fang ta b\u1eaft \u0111\u1ea7u \u0111i s\u00e2u v\u00e0o node js nh\u00e9<\/p>\n<p>C\u1ea3m \u01a1n c\u00e1c b\u1ea1n \u0111\u00e3 \u0111\u1ecdc b\u00e0i<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ch\u00e0o c\u00e1c b\u1ea1n ,h\u00f4m nay m\u00ecnh h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n vi\u1ebft ch\u01b0\u01a1ng tr\u00ecnh \u0111\u1ea7u ti\u00ean nh\u00e9: B\u01b0\u1edbc 1: B\u1ea1n t\u1ea1o m\u1ed9t th\u01b0 m\u1ee5c \u0111\u1ec3 l\u01b0u file code js nh\u00e9. B\u01b0\u1edbc 2: B\u1ea1n t\u1ea1o 1 file js v\u1edbi t\u00ean l\u00e0 &#8220;bai_1.js&#8221; B\u01b0\u1edbc 3: B\u1ea1n m\u1edf file &#8220;bai_1.js&#8221; b\u1eb1ng notepad++ n\u1ebfu b\u1ea1n ch\u01b0a c\u00f3 th\u00ec c\u00e0i v\u00e0o [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":230,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"amp_status":"","footnotes":""},"categories":[38,33],"tags":[58,59],"class_list":{"0":"post-229","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-node-js","8":"category-node-js-angular-js","9":"tag-node-js","10":"tag-node-js-http"},"_links":{"self":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/comments?post=229"}],"version-history":[{"count":1,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/229\/revisions"}],"predecessor-version":[{"id":231,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/229\/revisions\/231"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media\/230"}],"wp:attachment":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media?parent=229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/categories?post=229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/tags?post=229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}