{"id":83260,"date":"2025-01-02T14:18:50","date_gmt":"2025-01-02T07:18:50","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=83260"},"modified":"2025-09-28T23:09:00","modified_gmt":"2025-09-28T16:09:00","slug":"cau-hoi-phong-van-nodejs","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/","title":{"rendered":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS m\u1ecdi c\u1ea5p \u0111\u1ed9"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#Nodejs_la_gi_Tam_quan_trong_cua_Nodejs\" >Node.js l\u00e0 g\u00ec? T\u1ea7m quan tr\u1ecdng c\u1ee7a Node.js?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#Cau_hoi_phong_van_NodeJS_danh_cho_Fresher_Junior_Developer\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS d\u00e0nh cho Fresher\/ Junior Developer<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#Cau_hoi_phong_van_NodeJS_danh_cho_Middle_Developer\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS d\u00e0nh cho Middle Developer<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#Cau_hoi_phong_van_NodeJS_danh_cho_Senior_Developer\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS d\u00e0nh cho Senior Developer<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#Tong_ket_cau_hoi_phong_van_NodeJS\" >T\u1ed5ng k\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS<\/a><\/li><\/ul><\/nav><\/div>\n\n<p><em><strong>Trong b\u00e0i vi\u1ebft n\u00e0y, ITviec \u0111\u00e3 t\u1ed5ng h\u1ee3p danh s\u00e1ch \u0111\u1ea7y \u0111\u1ee7 c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS th\u01b0\u1eddng xu\u1ea5t hi\u1ec7n trong c\u00e1c cu\u1ed9c ph\u1ecfng v\u1ea5n, c\u00f9ng m\u1ed9t s\u1ed1 d\u1ea1ng \u0111\u1ec1 b\u00e0i th\u1ef1c h\u00e0nh l\u1eadp tr\u00ecnh v\u1edbi Node.js \u0111\u01a1n gi\u1ea3n. Tr\u01b0\u1edbc khi \u0111\u1ecdc \u0111\u00e1p \u00e1n, h\u00e3y th\u1eed t\u1ef1 m\u00ecnh tr\u1ea3 l\u1eddi tr\u01b0\u1edbc \u0111\u1ec3 &#8220;\u00f4n t\u1eadp&#8221; tr\u01b0\u1edbc.<\/strong><\/em><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ecdc b\u00e0i vi\u1ebft \u0111\u1ec3 hi\u1ec3u r\u00f5 h\u01a1n:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Node.js l\u00e0 g\u00ec? T\u1ea7m quan tr\u1ecdng c\u1ee7a Node.js?<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS cho Fresher\/ Junior Developer<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS cho Middle Developer<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS cho Senior Developer<\/span><\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Tham kh\u1ea3o <strong><a href=\"https:\/\/itviec.com\/it-jobs\/nodejs?utm_medium=anchor_text_high&amp;utm_source=blog&amp;utm_campaign=vec_lam_nodejs&amp;utm_content=cau_hoi_phong_van_nodejs\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m NodeJS<\/a><\/strong> m\u1edbi nh\u1ea5t tr\u00ean to\u00e0n qu\u1ed1c<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-node-js-la-gi-t\u1ea7m-quan-tr\u1ecdng-c\u1ee7a-node-js\"><span class=\"ez-toc-section\" id=\"Nodejs_la_gi_Tam_quan_trong_cua_Nodejs\"><\/span><b>Node.js l\u00e0 g\u00ec? T\u1ea7m quan tr\u1ecdng c\u1ee7a Node.js?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Node.js l\u00e0 m\u1ed9t m\u00f4i tr\u01b0\u1eddng m\u00e1y ch\u1ee7 m\u00e3 ngu\u1ed3n m\u1edf, s\u1eed d\u1ee5ng JavaScript tr\u00ean m\u00e1y ch\u1ee7. Node.js c\u00f3 th\u1ec3 ch\u1ea1y tr\u00ean nhi\u1ec1u n\u1ec1n t\u1ea3ng kh\u00e1c nhau (Windows, Linux, Unix, Mac OS X,&#8230;).<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Node.js r\u1ea5t quan tr\u1ecdng \u0111\u1ed1i v\u1edbi l\u1eadp tr\u00ecnh backend do kh\u1ea3 n\u0103ng x\u1eed l\u00fd c\u00e1c \u1ee9ng d\u1ee5ng hi\u1ec7u su\u1ea5t cao, th\u1eddi gian th\u1ef1c. \u0110\u1eb7c bi\u1ec7t, Node.js c\u00f2n \u0111\u01b0\u1ee3c bi\u1ebft \u0111\u1ebfn v\u1edbi ki\u1ebfn \u200b\u200btr\u00fac kh\u00f4ng ch\u1eb7n, h\u01b0\u1edbng s\u1ef1 ki\u1ec7n, n\u00ean r\u1ea5t ph\u00f9 h\u1ee3p \u0111\u1ec3 x\u1eed l\u00fd s\u1ed1 l\u01b0\u1ee3ng l\u1edbn c\u00e1c k\u1ebft n\u1ed1i \u0111\u1ed3ng th\u1eddi.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, Node.js h\u1ed7 tr\u1ee3 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng, x\u1eed l\u00fd d\u1eef li\u1ec7u nhanh v\u00e0 l\u00fd t\u01b0\u1edfng \u0111\u1ec3 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng nh\u01b0 API, \u1ee9ng d\u1ee5ng tr\u00f2 chuy\u1ec7n v\u00e0 n\u1ec1n t\u1ea3ng c\u1ed9ng t\u00e1c th\u1eddi gian th\u1ef1c.<\/span><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>\u0110\u1ecdc th\u00eam: <a href=\"https:\/\/itviec.com\/blog\/nodejs-backend-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>NodeJS Backend: Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng NodeJS l\u1eadp tr\u00ecnh backend?<\/strong><\/a><\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-nodejs-danh-cho-fresher-junior-developer\"><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_NodeJS_danh_cho_Fresher_Junior_Developer\"><\/span><b>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS d\u00e0nh cho Fresher\/ Junior Developer<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-s\u1ef1-khac-bi\u1ec7t-gi\u1eefa-node-js-va-javascript-trong-trinh-duy\u1ec7t-la-gi\"><b>S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa Node.js v\u00e0 JavaScript trong tr\u00ecnh duy\u1ec7t l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">JavaScript l\u00e0 ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh ch\u1ee7 y\u1ebfu \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 ph\u00e1t tri\u1ec3n web ph\u00eda m\u00e1y kh\u00e1ch (client-side), trong khi Node l\u00e0 m\u00f4i tr\u01b0\u1eddng th\u1eddi gian ch\u1ea1y (runtime) cho ph\u00e9p JavaScript \u0111\u01b0\u1ee3c th\u1ef1c thi \u1edf ph\u00eda m\u00e1y ch\u1ee7.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>&nbsp;<\/td><td><b>Javascript<\/b><\/td><td><b>NodeJS<\/b><\/td><\/tr><tr><td><b>Kh\u00e1i ni\u1ec7m<\/b><\/td><td><span style=\"font-weight: 400;\">Ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 vi\u1ebft c\u00e1c t\u1eadp l\u1ec7nh tr\u00ean trang web<\/span><\/td><td><span style=\"font-weight: 400;\">M\u00f4i tr\u01b0\u1eddng ch\u1ea1y Javascript<\/span><\/td><\/tr><tr><td><b>M\u00f4i tr\u01b0\u1eddng ch\u1ea1y<\/b><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 c\u00f3 th\u1ec3 ch\u1ea1y trong tr\u00ecnh duy\u1ec7t<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ea1y tr\u00ean m\u00e1y ch\u1ee7 (server-side), s\u1eed d\u1ee5ng V8 JavaScript engine c\u1ee7a Google Chrome.<\/span><\/td><\/tr><tr><td><b>V\u1ecb tr\u00ed s\u1eed d\u1ee5ng<\/b><\/td><td><span style=\"font-weight: 400;\">Ph\u00eda m\u00e1y kh\u00e1ch<\/span><\/td><td><span style=\"font-weight: 400;\">Ph\u00eda m\u00e1y ch\u1ee7 (server-side)<\/span>\n<p><span style=\"font-weight: 400;\">C\u00f4ng c\u1ee5 d\u00f2ng l\u1ec7nh (CLI tools)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u1ee8ng d\u1ee5ng desktop (v\u1edbi Electron)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ph\u00e1t tri\u1ec3n IoT<\/span><\/p>\n<\/td><\/tr><tr><td><b>Kh\u1ea3 n\u0103ng th\u00eam th\u1ebb HTML<\/b><\/td><td><span style=\"font-weight: 400;\">\u0110\u1ee7 kh\u1ea3 n\u0103ng \u0111\u1ec3 th\u00eam HTML v\u00e0 DOM<\/span><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng c\u00f3 kh\u1ea3 n\u0103ng th\u00eam th\u1ebb HTM<\/span><\/td><\/tr><tr><td><b>Kh\u1ea3 n\u0103ng ch\u1ea1y trong tr\u00ecnh duy\u1ec7t<\/b><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ch\u1ea1y trong b\u1ea5t k\u1ef3 c\u00f4ng c\u1ee5 tr\u00ecnh duy\u1ec7t n\u00e0o nh\u01b0 JS core trong Safari v\u00e0 Spidermonkey trong Firefox<\/span><\/td><td><span style=\"font-weight: 400;\">V8 l\u00e0 engine m\u1eb7c \u0111\u1ecbnh c\u1ee7a Node.js. Nh\u01b0ng Node.js kh\u00f4ng ch\u1ea1y trong tr\u00ecnh duy\u1ec7t, n\u00f3 ch\u1ea1y \u0111\u1ed9c l\u1eadp tr\u00ean m\u00f4i tr\u01b0\u1eddng m\u00e1y t\u00ednh<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-node-js-th\u01b0\u1eddng-s\u1eed-d\u1ee5ng-cac-th\u01b0-vi\u1ec7n-nao\"><b>Node.js th\u01b0\u1eddng s\u1eed d\u1ee5ng c\u00e1c th\u01b0 vi\u1ec7n n\u00e0o?<\/b><i><\/i><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00f3 kh\u00e1 nhi\u1ec1u th\u01b0 vi\u1ec7n th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong Node.js, trong \u0111\u00f3 c\u00f3 th\u1ec3 k\u1ec3 \u0111\u1ebfn:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>ExpressJS &#8211; Express<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t n\u1ec1n t\u1ea3ng \u1ee9ng d\u1ee5ng web Node.js linh ho\u1ea1t cung c\u1ea5p nhi\u1ec1u t\u00ednh n\u0103ng \u0111\u1ec3 ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng web v\u00e0 di \u0111\u1ed9ng. ExpressJS th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng web \u0111\u1ed9ng, API RESTful v\u00e0 c\u00e1c \u1ee9ng d\u1ee5ng m\u1ed9t trang (single-page application).<\/span><\/li>\n\n\n\n<li><b>Mongoose &#8211; Mongoose<\/b><span style=\"font-weight: 400;\"> c\u0169ng l\u00e0 m\u1ed9t n\u1ec1n t\u1ea3ng \u1ee9ng d\u1ee5ng web Node.js gi\u00fap k\u1ebft n\u1ed1i \u1ee9ng d\u1ee5ng v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-node-js-ho\u1ea1t-d\u1ed9ng-nh\u01b0-th\u1ebf-nao\"><b>Node.js ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Khi m\u00e1y kh\u00e1ch g\u1eedi y\u00eau c\u1ea7u \u0111\u1ebfn m\u00e1y ch\u1ee7 web \u0111\u1ec3 t\u01b0\u01a1ng t\u00e1c v\u1edbi \u1ee9ng d\u1ee5ng web, y\u00eau c\u1ea7u c\u00f3 th\u1ec3 l\u00e0 kh\u00f4ng ch\u1eb7n ho\u1eb7c ch\u1eb7n, ch\u1eb3ng h\u1ea1n truy v\u1ea5n d\u1eef li\u1ec7u, x\u00f3a ho\u1eb7c c\u1eadp nh\u1eadt d\u1eef li\u1ec7u, Node.js s\u1ebd ho\u1ea1t \u0111\u1ed9ng nh\u01b0 sau:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Node.js truy xu\u1ea5t c\u00e1c y\u00eau c\u1ea7u \u0111\u1ebfn v\u00e0 th\u00eam ch\u00fang v\u00e0o H\u00e0ng \u0111\u1ee3i s\u1ef1 ki\u1ec7n<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e1c y\u00eau c\u1ea7u \u0111\u01b0\u1ee3c x\u1eed l\u00fd theo t\u1eebng y\u00eau c\u1ea7u m\u1ed9t th\u00f4ng qua V\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n. V\u00f2ng l\u1eb7p n\u00e0y ki\u1ec3m tra xem c\u00e1c y\u00eau c\u1ea7u c\u00f3 \u0111\u01a1n gi\u1ea3n v\u00e0 kh\u00f4ng c\u1ea7n t\u00e0i nguy\u00ean b\u00ean ngo\u00e0i hay kh\u00f4ng.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">V\u1edbi c\u00e1c y\u00eau c\u1ea7u \u0111\u01a1n gi\u1ea3n (ho\u1ea1t \u0111\u1ed9ng kh\u00f4ng ch\u1eb7n), nh\u01b0 thao t\u00e1c I\/O, v\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n s\u1ebd x\u1eed l\u00fd ngay l\u1eadp t\u1ee9c v\u00e0 tr\u1ea3 v\u1ec1 ph\u1ea3n h\u1ed3i cho m\u00e1y kh\u00e1ch m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i ch\u1edd \u0111\u1ee3i.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">\u0110\u1ed1i v\u1edbi c\u00e1c y\u00eau c\u1ea7u ph\u1ee9c t\u1ea1p, v\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n s\u1ebd ch\u1ec9 \u0111\u1ecbnh m\u1ed9t lu\u1ed3ng t\u1eeb Nh\u00f3m lu\u1ed3ng \u0111\u1ec3 x\u1eed l\u00fd y\u00eau c\u1ea7u \u0111\u00f3. Lu\u1ed3ng n\u00e0y s\u1ebd ch\u1ecbu tr\u00e1ch nhi\u1ec7m ho\u00e0n th\u00e0nh c\u00e1c y\u00eau c\u1ea7u ch\u1eb7n, v\u00ed d\u1ee5 nh\u01b0 truy c\u1eadp c\u01a1 s\u1edf d\u1eef li\u1ec7u, h\u1ec7 th\u1ed1ng t\u1ec7p ho\u1eb7c th\u1ef1c hi\u1ec7n t\u00ednh to\u00e1n.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Sau khi y\u00eau c\u1ea7u \u0111\u01b0\u1ee3c ho\u00e0n th\u00e0nh, ph\u1ea3n h\u1ed3i s\u1ebd \u0111\u01b0\u1ee3c \u0111\u01b0a l\u1ea1i v\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n v\u00e0 g\u1eedi \u0111\u1ebfn m\u00e1y kh\u00e1ch.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-node-js-la-d\u01a1n-lu\u1ed3ng-hay-da-lu\u1ed3ng\"><b>Node.js l\u00e0 \u0111\u01a1n lu\u1ed3ng hay \u0111a lu\u1ed3ng?<\/b><i><\/i><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Node.js s\u1eed d\u1ee5ng m\u00f4 h\u00ecnh <\/span><b>lu\u1ed3ng \u0111\u01a1n<\/b><span style=\"font-weight: 400;\">, c\u00f3 ngh\u0129a l\u00e0 n\u00f3 ch\u1ec9 s\u1eed d\u1ee5ng m\u1ed9t lu\u1ed3ng duy nh\u1ea5t \u0111\u1ec3 x\u1eed l\u00fd nhi\u1ec1u t\u00e1c v\u1ee5. Ki\u1ebfn tr\u00fac lu\u1ed3ng \u0111\u01a1n c\u1ee7a Node.js \u0111\u01b0\u1ee3c \u0111i\u1ec1u khi\u1ec3n b\u1edfi v\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n v\u00e0 I\/O kh\u00f4ng ch\u1eb7n. \u0110\u00e2y l\u00e0 m\u1ed9t l\u1ef1a ch\u1ecdn thi\u1ebft k\u1ebf c\u00f3 ch\u1ee7 \u0111\u00edch c\u00e2n b\u1eb1ng gi\u1eefa t\u00ednh \u0111\u01a1n gi\u1ea3n, hi\u1ec7u su\u1ea5t v\u00e0 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-n\u1ebfu-node-js-la-lu\u1ed3ng-d\u01a1n-v\u1eady-no-x\u1eed-ly-d\u1ed3ng-th\u1eddi-nh\u01b0-th\u1ebf-nao\"><b>N\u1ebfu Node.js l\u00e0 lu\u1ed3ng \u0111\u01a1n, v\u1eady n\u00f3 x\u1eed l\u00fd \u0111\u1ed3ng th\u1eddi nh\u01b0 th\u1ebf n\u00e0o?<\/b><i><\/i><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 l\u00e0m \u0111\u01b0\u1ee3c \u0111i\u1ec1u n\u00e0y, Node.js \u00e1p d\u1ee5ng m\u00f4 h\u00ecnh <\/span><b>I\/O kh\u00f4ng ch\u1eb7n<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>h\u01b0\u1edbng s\u1ef1 ki\u1ec7n<\/b><span style=\"font-weight: 400;\">, gi\u00fap x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 m\u00e0 kh\u00f4ng ph\u1ea3i ch\u1edd \u0111\u1ee3i nhau, t\u1eeb \u0111\u00f3 t\u0103ng hi\u1ec7u su\u1ea5t v\u00e0 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-node-js-ajax-va-jquery-co-gi-khac-nhau\"><b>Node.js, AJAX v\u00e0 jQuery c\u00f3 g\u00ec kh\u00e1c nhau?<\/b><i><\/i><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">M\u1ed9t \u0111\u1eb7c \u0111i\u1ec3m chung gi\u1eefa Node.js, AJAX v\u00e0 jQuery l\u00e0 c\u1ea3 3 \u0111\u1ec1u s\u1eed d\u1ee5ng&nbsp; JavaScript. Tuy nhi\u00ean, ch\u00fang ph\u1ee5c v\u1ee5 c\u00e1c m\u1ee5c \u0111\u00edch ho\u00e0n to\u00e0n kh\u00e1c nhau.<\/span><\/p>\n\n\n\n<p><b>Node.js: <\/b><span style=\"font-weight: 400;\">L\u00e0 n\u1ec1n t\u1ea3ng ph\u00eda m\u00e1y ch\u1ee7 \u0111\u1ec3 ph\u00e1t tri\u1ec3n c\u00e1c \u1ee9ng d\u1ee5ng m\u00e1y kh\u00e1ch &#8211; m\u00e1y ch\u1ee7. V\u00ed d\u1ee5, n\u1ebfu ph\u1ea3i x\u00e2y d\u1ef1ng m\u1ed9t h\u1ec7 th\u1ed1ng qu\u1ea3n l\u00fd nh\u00e2n vi\u00ean tr\u1ef1c tuy\u1ebfn, th\u00ec l\u1eadp tr\u00ecnh vi\u00ean s\u1ebd kh\u00f4ng th\u1ef1c hi\u1ec7n b\u1eb1ng JS ph\u00eda m\u00e1y kh\u00e1ch.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Nh\u01b0ng Node.js ch\u1eafc ch\u1eafn c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n \u0111\u01b0\u1ee3c v\u00ec n\u00f3 ch\u1ea1y tr\u00ean m\u00e1y ch\u1ee7 t\u01b0\u01a1ng t\u1ef1 nh\u01b0 Apache, Django ch\u1ee9 kh\u00f4ng ph\u1ea3i tr\u00ean tr\u00ecnh duy\u1ec7t.<\/span><\/p>\n\n\n\n<p><b>AJAX (hay c\u00f2n g\u1ecdi l\u00e0 Javascript v\u00e0 XML kh\u00f4ng \u0111\u1ed3ng b\u1ed9)<\/b><span style=\"font-weight: 400;\">: L\u00e0 k\u1ef9 thu\u1eadt vi\u1ebft k\u1ecbch b\u1ea3n ph\u00eda m\u00e1y kh\u00e1ch, ch\u1ee7 y\u1ebfu \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1ec3 hi\u1ec3n th\u1ecb n\u1ed9i dung c\u1ee7a m\u1ed9t trang m\u00e0 kh\u00f4ng c\u1ea7n l\u00e0m m\u1edbi trang.<\/span><\/p>\n\n\n\n<p><b>jQuery: <\/b><span style=\"font-weight: 400;\">L\u00e0 m\u1ed9t m\u00f4-\u0111un JavaScript n\u1ed5i ti\u1ebfng b\u1ed5 sung cho AJAX, DOM traversal, v\u00f2ng l\u1eb7p,&#8230; Th\u01b0 vi\u1ec7n n\u00e0y cung c\u1ea5p nhi\u1ec1u h\u00e0m h\u1eefu \u00edch \u0111\u1ec3 h\u1ed7 tr\u1ee3 ph\u00e1t tri\u1ec3n JavaScript.&nbsp;<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-node-js-h\u1ed7-tr\u1ee3-lo\u1ea1i-ham-api-nao\"><b>Node.js h\u1ed7 tr\u1ee3 lo\u1ea1i h\u00e0m API n\u00e0o?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00f3 hai lo\u1ea1i h\u00e0m API \u0111\u01b0\u1ee3c Node.js h\u1ed7 tr\u1ee3:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>\u0110\u1ed3ng b\u1ed9 (Synchronous):<\/b><span style=\"font-weight: 400;\"> C\u00e1c h\u00e0m API n\u00e0y \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 ch\u1eb7n m\u00e3.<\/span><\/li>\n\n\n\n<li><b>Kh\u00f4ng \u0111\u1ed3ng b\u1ed9 (Asynchronous):<\/b><span style=\"font-weight: 400;\"> C\u00e1c h\u00e0m API n\u00e0y \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng cho m\u00e3 kh\u00f4ng ch\u1eb7n.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-b\u1ea1n-hi\u1ec3u-thu\u1eadt-ng\u1eef-i-o-la-nh\u01b0-th\u1ebf-nao\"><b>B\u1ea1n hi\u1ec3u thu\u1eadt ng\u1eef I\/O l\u00e0 nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Thu\u1eadt ng\u1eef I\/O (Input\/Output) \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 m\u00f4 t\u1ea3 b\u1ea5t k\u1ef3 ch\u01b0\u01a1ng tr\u00ecnh, ho\u1ea1t \u0111\u1ed9ng, ho\u1eb7c thi\u1ebft b\u1ecb n\u00e0o c\u00f3 kh\u1ea3 n\u0103ng truy\u1ec1n d\u1eef li\u1ec7u t\u1eeb ho\u1eb7c \u0111\u1ebfn m\u1ed9t ph\u01b0\u01a1ng ti\u1ec7n n\u00e0y v\u00e0 t\u1eeb ho\u1eb7c \u0111\u1ebfn m\u1ed9t ph\u01b0\u01a1ng ti\u1ec7n kh\u00e1c.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">M\u1ed7i l\u1ea7n truy\u1ec1n d\u1eef li\u1ec7u \u0111\u1ec1u bao g\u1ed3m m\u1ed9t \u0111\u1ea7u ra t\u1eeb m\u1ed9t ph\u01b0\u01a1ng ti\u1ec7n v\u00e0 m\u1ed9t \u0111\u1ea7u v\u00e0o v\u00e0o m\u1ed9t ph\u01b0\u01a1ng ti\u1ec7n kh\u00e1c. C\u00e1c ph\u01b0\u01a1ng ti\u1ec7n n\u00e0y c\u00f3 th\u1ec3 l\u00e0 thi\u1ebft b\u1ecb v\u1eadt l\u00fd, m\u1ea1ng ho\u1eb7c c\u00e1c t\u1ec7p trong m\u1ed9t h\u1ec7 th\u1ed1ng.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-hay-gi\u1ea3i-thich-npm-install-va-npm-ci\"><b>H\u00e3y gi\u1ea3i th\u00edch npm install v\u00e0 npm ci?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">npm i (ho\u1eb7c npm install) \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 c\u00e0i \u0111\u1eb7t t\u1ea5t c\u1ea3 c\u00e1c dependency ho\u1eb7c devDependencies t\u1eeb t\u1ec7p package.json. N\u00f3 c\u00f3 th\u1ec3 c\u00e0i \u0111\u1eb7t th\u00eam package, s\u1eeda \u0111\u1ed5i v\u00e0 c\u1eadp nh\u1eadt phi\u00ean b\u1ea3n v\u00e0 c\u0169ng c\u00f3 th\u1ec3 c\u1eadp nh\u1eadt t\u1ec7p package-lock.json.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">npm install \"package-name\"<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">ho\u1eb7c:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">npm i \"package-name\"<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">CI l\u00e0 vi\u1ebft t\u1eaft c\u1ee7a clean install v\u00e0 npm ci \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 c\u00e0i \u0111\u1eb7t t\u1ea5t c\u1ea3 dependency phi\u00ean b\u1ea3n ch\u00ednh x\u00e1c ho\u1eb7c devDependencies t\u1eeb t\u1ec7p package-lock.json. N\u00f3 kh\u00f4ng th\u1ec3 c\u00e0i \u0111\u1eb7t c\u00e1c g\u00f3i b\u1ed5 sung ho\u1eb7c s\u1eeda \u0111\u1ed5i c\u00e1c dependency \u0111\u00e3 c\u00e0i \u0111\u1eb7t. C\u00fa ph\u00e1p: <\/span><span style=\"font-weight: 400;\">npm ci<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-mo-dun-module-trong-node-js-la-gi-lam-th\u1ebf-nao-d\u1ec3-import-export-m\u1ed9t-module\"><b>M\u00f4-\u0111un (module) trong Node.js l\u00e0 g\u00ec? L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 import\/export m\u1ed9t module?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00e1c m\u00f4-\u0111un trong Node.js gi\u1ed1ng nh\u01b0 c\u00e1c th\u01b0 vi\u1ec7n JavaScript, cung c\u1ea5p m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c h\u00e0m m\u00e0 b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng trong \u1ee9ng d\u1ee5ng c\u1ee7a m\u00ecnh.&nbsp;<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">\u0110\u1ec3 chia s\u1ebb m\u1ed9t ph\u1ea7n m\u00e3 (ch\u1eb3ng h\u1ea1n nh\u01b0 m\u1ed9t h\u00e0m ho\u1eb7c \u0111\u1ed1i t\u01b0\u1ee3ng) t\u1eeb m\u00f4-\u0111un n\u00e0y sang m\u00f4-\u0111un kh\u00e1c, b\u1ea1n s\u1eed d\u1ee5ng module.exports trong m\u00f4-\u0111un.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">\u0110\u1ec3 s\u1eed d\u1ee5ng m\u1ed9t m\u00f4-\u0111un \u0111\u00e3 \u0111\u01b0\u1ee3c export \u1edf n\u01a1i kh\u00e1c, b\u1ea1n d\u00f9ng require() \u0111\u1ec3 import m\u00f4-\u0111un \u0111\u00f3 v\u00e0o trong m\u00e3 c\u1ee7a b\u1ea1n.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-vi\u1ebft-hello-world-trong-node-js\"><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 vi\u1ebft \u201cHello world\u201d trong Node.js<\/b><i><\/i><\/h3>\n\n\n\n<p><b>B\u01b0\u1edbc 1:<\/b><span style=\"font-weight: 400;\"> Kh\u1edfi t\u1ea1o \u1ee9ng d\u1ee5ng NodeJS b\u1eb1ng l\u1ec7nh<\/span><span style=\"font-weight: 400;\"> npm init<\/span><\/p>\n\n\n\n<p><b>B\u01b0\u1edbc 2: <\/b><span style=\"font-weight: 400;\">Tri\u1ec3n khai module express trong d\u1ef1 \u00e1n c\u1ee7a b\u1ea1n b\u1eb1ng l\u1ec7nh <\/span><span style=\"font-weight: 400;\">npm install express<\/span><span style=\"font-weight: 400;\">. C\u00e1c ph\u1ee5 thu\u1ed9c (dependencies) \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt trong&nbsp; t\u1ec7p package.json s\u1ebd nh\u01b0 th\u1ebf n\u00e0y:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">\"dependencies\": {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;\"express\": \"^4.19.2\"<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;}<\/span><\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 3: <\/b><span style=\"font-weight: 400;\">Ch\u1ea1y t\u1ec7p app.js b\u1eb1ng l\u1ec7nh node app.js<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">\/\/ app.js<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ Require would make available the<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ express package to be used in<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ our code<\/span>\n\n<span style=\"font-weight: 400;\">const express = require(\"express\");<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ Creates an express object<\/span>\n\n<span style=\"font-weight: 400;\">const app = express();<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ It listens to HTTP get request.&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ Here it listens to the root i.e '\/'<\/span>\n\n<span style=\"font-weight: 400;\">app.get(\"\/\", (req, res) =&gt; {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;\/\/ Using send function we send<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;\/\/ response to the client<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;\/\/ Here we are sending html<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;res.send(\"&lt;h1&gt; Hello World &lt;\/h1&gt;\");<\/span>\n\n<span style=\"font-weight: 400;\">});<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ It configures the system to listen<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ to port 3000. Any number can be&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ given instead of 3000, the only<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ condition is that no other server<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ should be running at that port<\/span>\n\n<span style=\"font-weight: 400;\">app.listen(3000, () =&gt; {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;\/\/ Print in the console when the<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;\/\/ servers starts to listen on 3000<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;console.log(\"Listening to port 3000\");<\/span>\n\n<span style=\"font-weight: 400;\">});<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">B\u00e2y gi\u1edd h\u00e3y m\u1edf tr\u00ecnh duy\u1ec7t c\u1ee7a b\u1ea1n v\u00e0 truy c\u1eadp v\u00e0o http:\/\/localhost:3000\/, b\u1ea1n s\u1ebd th\u1ea5y k\u1ebft qu\u1ea3 nh\u01b0 mong \u0111\u1ee3i.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-s\u1ef1-khac-bi\u1ec7t-gi\u1eefa-require-va-import-trong-node-js-la-gi\"><b>S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa require v\u00e0 import trong Node.js l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>&nbsp;<\/td><td><b>require()<\/b><\/td><td><b>import<\/b><\/td><\/tr><tr><td><b>C\u00fa ph\u00e1p<\/b><\/td><td><span style=\"font-weight: 400;\">CommonJS: require(&#8216;module&#8217;)<\/span><\/td><td><span style=\"font-weight: 400;\">ES6: import module from &#8216;module&#8217;<\/span><\/td><\/tr><tr><td><b>M\u1ee5c \u0111\u00edch s\u1eed d\u1ee5ng<\/b><\/td><td><span style=\"font-weight: 400;\">Nh\u1eadp module \u0111\u1ed3ng b\u1ed9<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 \u0111\u1ed3ng b\u1ed9 ho\u1eb7c kh\u00f4ng \u0111\u1ed3ng b\u1ed9, t\u00f9y v\u00e0o c\u00e1ch s\u1eed d\u1ee5ng<\/span><\/td><\/tr><tr><td><b>H\u1ed7 tr\u1ee3 module<\/b><\/td><td><span style=\"font-weight: 400;\">&#8211; H\u1ed7 tr\u1ee3 c\u1ea3 module CommonJS v\u00e0 module ES<\/span>\n<p><span style=\"font-weight: 400;\">&#8211; \u0110\u1ed3ng b\u1ed9, t\u1ea3i module theo y\u00eau c\u1ea7u<\/span><\/p>\n<\/td><td><span style=\"font-weight: 400;\">&#8211; \u1ed7 tr\u1ee3 module ES (y\u00eau c\u1ea7u ph\u1ea7n m\u1edf r\u1ed9ng .mjs) ho\u1eb7c .js n\u1ebfu \u0111\u01b0\u1ee3c c\u1ea5u h\u00ecnh \u0111\u00fang.<\/span>\n<p><span style=\"font-weight: 400;\">&#8211; Kh\u00f4ng \u0111\u1ed3ng b\u1ed9, ch\u1ec9 t\u1ea3i module khi c\u1ea7n<\/span><\/p>\n<\/td><\/tr><tr><td><b>Kh\u1ea3 n\u0103ng export<\/b><\/td><td><span style=\"font-weight: 400;\">&#8211; H\u1ed7 tr\u1ee3 export c\u00f3 t\u00ean th\u00f4ng qua destructuring<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211; H\u1ed7 tr\u1ee3 export m\u1eb7c \u0111\u1ecbnh (import module from &#8216;module&#8217;)<\/span>\n<p><span style=\"font-weight: 400;\">&#8211; H\u1ed7 tr\u1ee3 export c\u00f3 t\u00ean tr\u1ef1c ti\u1ebfp (import { name } from &#8216;module&#8217;)<\/span><\/p>\n<\/td><\/tr><tr><td><b>Kh\u1ea3 n\u0103ng ph\u00e2n t\u00edch<\/b><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng h\u1ed7 tr\u1ee3 ph\u00e2n t\u00edch t\u0129nh<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 ph\u00e2n t\u00edch t\u0129nh (cho ph\u00e9p enables tree shaking)<\/span><\/td><\/tr><tr><td><b>Kh\u1ea3 n\u0103ng import<\/b><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng h\u1ed7 tr\u1ee3 import \u0111\u1ed9ng<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 import \u0111\u1ed9ng (h\u00e0m import())<\/span><\/td><\/tr><tr><td><b>M\u00f4i tr\u01b0\u1eddng tr\u00ecnh duy\u1ec7t<\/b><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng \u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3 trong m\u00f4i tr\u01b0\u1eddng tr\u00ecnh duy\u1ec7t<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c h\u1ed7 tr\u1ee3 trong c\u00e1c tr\u00ecnh duy\u1ec7t hi\u1ec7n \u0111\u1ea1i c\u00f3 h\u1ed7 tr\u1ee3 module<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-d\u1ecdc-t\u1ec7p-files-trong-node-js\"><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 \u0111\u1ecdc t\u1ec7p (files) trong Node.js?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 \u0111\u1ecdc m\u1ed9t t\u1ec7p trong NodeJS, c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c fs.readFile(). Ph\u01b0\u01a1ng th\u1ee9c n\u00e0y \u0111\u1ecdc kh\u00f4ng \u0111\u1ed3ng b\u1ed9 to\u00e0n b\u1ed9 n\u1ed9i dung c\u1ee7a t\u1ec7p v\u00e0 truy\u1ec1n d\u1eef li\u1ec7u \u0111\u1ebfn h\u00e0m callback.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">const fs = require('fs');<\/span>\n<span style=\"font-weight: 400;\">fs.readFile('example.txt', 'utf8', (error, data) =&gt; {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;if (error) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.error('An error occurred while reading the file:', error);<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('File content:', data);<\/span>\n<span style=\"font-weight: 400;\">});<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-ghi-t\u1ec7p-files-trong-node-js\"><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 ghi t\u1ec7p (files) trong Node.js?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 ghi v\u00e0o m\u1ed9t t\u1ec7p trong NodeJS, c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c fs.writeFile(). Ph\u01b0\u01a1ng th\u1ee9c n\u00e0y ghi d\u1eef li\u1ec7u kh\u00f4ng \u0111\u1ed3ng b\u1ed9 v\u00e0o m\u1ed9t t\u1ec7p, thay th\u1ebf t\u1ec7p n\u1ebfu t\u1ec7p \u0111\u00e3 t\u1ed3n t\u1ea1i ho\u1eb7c t\u1ea1o t\u1ec7p m\u1edbi n\u1ebfu t\u1ec7p ch\u01b0a t\u1ed3n t\u1ea1i.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">const fs = require('fs');<\/span>\n<span style=\"font-weight: 400;\">const content = '\u0110\u00e2y l\u00e0 n\u1ed9i dung s\u1ebd ghi trong t\u1ec7p.';<\/span>\n<span style=\"font-weight: 400;\">fs.writeFile('example.txt', content, 'utf8', (error) =&gt; {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;if (error) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.error('M\u1ed9t l\u1ed7i \u0111\u00e3 xu\u1ea5t hi\u1ec7n khi ghi t\u1ec7p:', error);<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('T\u1ec7p \u0111\u00e3 \u0111\u01b0\u1ee3c ghi th\u00e0nh c\u00f4ng.');<\/span>\n<span style=\"font-weight: 400;\">});<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-stream-trong-node-js-la-gi-co-nh\u1eefng-lo\u1ea1i-stream-nao\"><b>Stream trong Node.js l\u00e0 g\u00ec? C\u00f3 nh\u1eefng lo\u1ea1i stream n\u00e0o?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Lu\u1ed3ng (Stream) l\u00e0 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng cho ph\u00e9p l\u1eadp tr\u00ecnh vi\u00ean \u0111\u1ecdc d\u1eef li\u1ec7u t\u1eeb m\u1ed9t ngu\u1ed3n ho\u1eb7c ghi d\u1eef li\u1ec7u v\u00e0o \u0111\u00edch theo c\u00e1ch li\u00ean t\u1ee5c. Trong Node.js, c\u00f3 b\u1ed1n lo\u1ea1i lu\u1ed3ng:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 \u0111\u1ecdc (Readable): Lu\u1ed3ng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng cho ho\u1ea1t \u0111\u1ed9ng \u0111\u1ecdc.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ghi (Writable): Lu\u1ed3ng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng cho ho\u1ea1t \u0111\u1ed9ng ghi.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Song c\u00f4ng (Duplex): Lu\u1ed3ng c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng cho c\u1ea3 ho\u1ea1t \u0111\u1ed9ng \u0111\u1ecdc v\u00e0 ghi.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u1ed5i (Transform): Lu\u1ed3ng song c\u00f4ng trong \u0111\u00f3 \u0111\u1ea7u ra \u0111\u01b0\u1ee3c t\u00ednh to\u00e1n d\u1ef1a tr\u00ean \u0111\u1ea7u v\u00e0o.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-b\u1ea1n-co-bi\u1ebft-di\u1ec3m-khac-bi\u1ec7t-nao-gi\u1eefa-node-js-va-angular-js-khong\"><b>B\u1ea1n c\u00f3 bi\u1ebft \u0111i\u1ec3m kh\u00e1c bi\u1ec7t n\u00e0o gi\u1eefa Node.js v\u00e0 Angular.js kh\u00f4ng?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Angular l\u00e0 m\u1ed9t framework d\u00e0nh cho l\u1eadp tr\u00ecnh Frontend, \u0111\u01b0\u1ee3c vi\u1ebft b\u1eb1ng TypeScript v\u00e0 th\u01b0\u1eddng s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng web ph\u00eda m\u00e1y kh\u00e1ch ho\u1eb7c \u1ee9ng d\u1ee5ng \u0111\u01a1n trang (SPA) b\u1eb1ng c\u00e1ch chia m\u1ed9t \u1ee9ng d\u1ee5ng web th\u00e0nh c\u00e1c th\u00e0nh ph\u1ea7n MVC, ngo\u00e0i ra c\u00f2n h\u1ed7 tr\u1ee3 MVVM v\u00e0 component-based architecture.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Node.js \u00e0 m\u1ed9t m\u00f4i tr\u01b0\u1eddng ph\u00eda m\u00e1y ch\u1ee7, \u0111\u01b0\u1ee3c vi\u1ebft b\u1eb1ng ng\u00f4n ng\u1eef C, C++, th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng m\u1ea1ng ph\u00eda m\u00e1y ch\u1ee7 nhanh v\u00e0 c\u00f3 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng b\u1eb1ng c\u00e1ch t\u1ea1o c\u00e1c truy v\u1ea5n c\u01a1 s\u1edf d\u1eef li\u1ec7u, x\u00e2y d\u1ef1ng web server, REST APIs v\u00e0 nhi\u1ec1u \u1ee9ng d\u1ee5ng kh\u00e1c.<\/span><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>\u0110\u1ecdc th\u00eam:&nbsp;<a href=\"https:\/\/itviec.com\/blog\/nodejs-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>Top 5 NodeJS framework<\/strong><\/a><\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-nodejs-danh-cho-middle-developer\"><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_NodeJS_danh_cho_Middle_Developer\"><\/span><b>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS d\u00e0nh cho Middle Developer<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nbsp-m\u1ee5c-dich-c\u1ee7a-l\u1edbp-class-buffer-trong-node-js-la-gi\"><b>&nbsp;M\u1ee5c \u0111\u00edch c\u1ee7a l\u1edbp (class) Buffer trong Node.js l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u1edbp Buffer l\u00e0 m\u1ed9t l\u1edbp to\u00e0n c\u1ee5c, c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp trong \u1ee9ng d\u1ee5ng m\u00e0 kh\u00f4ng c\u1ea7n import m\u00f4-\u0111un buffer. Buffer l\u00e0 m\u1ed9t lo\u1ea1i m\u1ea3ng s\u1ed1 nguy\u00ean v\u00e0 t\u01b0\u01a1ng \u1ee9ng v\u1edbi ph\u00e2n b\u1ed5 b\u1ed9 nh\u1edb th\u00f4 b\u00ean ngo\u00e0i heap V8. Kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc c\u1ee7a l\u1edbp n\u00e0y.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-middleware-trong-node-js-la-gi\"><b>Middleware trong Node.js l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Middleware l\u00e0 ch\u1ee9c n\u0103ng nh\u1eadn c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng y\u00eau c\u1ea7u v\u00e0 ph\u1ea3n h\u1ed3i. Middleware \u0111\u01b0\u1ee3c th\u1ef1c thi sau khi m\u00e1y ch\u1ee7 nh\u1eadn \u0111\u01b0\u1ee3c y\u00eau c\u1ea7u v\u00e0 tr\u01b0\u1edbc khi b\u1ed9 \u0111i\u1ec1u khi\u1ec3n g\u1eedi ph\u1ea3n h\u1ed3i. C\u00e1c ho\u1ea1t \u0111\u1ed9ng ch\u00ednh c\u1ee7a ch\u1ee9c n\u0103ng n\u00e0y g\u1ed3m:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Th\u1ef1c thi code b\u1ea5t k\u1ef3<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u1eadp nh\u1eadt ho\u1eb7c s\u1eeda \u0111\u1ed5i c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng y\u00eau c\u1ea7u v\u00e0 ph\u1ea3n h\u1ed3i<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">K\u1ebft th\u00fac chu k\u1ef3 y\u00eau c\u1ea7u-ph\u1ea3n h\u1ed3i<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">G\u1ecdi middleware ti\u1ebfp theo trong ng\u0103n x\u1ebfp<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-l\u1eadp-trinh-h\u01b0\u1edbng-s\u1ef1-ki\u1ec7n-trong-node-js-la-gi\"><b>L\u1eadp tr\u00ecnh h\u01b0\u1edbng s\u1ef1 ki\u1ec7n trong Node.js l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u1eadp tr\u00ecnh h\u01b0\u1edbng s\u1ef1 ki\u1ec7n (Event-driven programming) \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u1ed3ng b\u1ed9 h\u00f3a s\u1ef1 xu\u1ea5t hi\u1ec7n c\u1ee7a nhi\u1ec1u s\u1ef1 ki\u1ec7n v\u00e0 l\u00e0m ch\u01b0\u01a1ng tr\u00ecnh \u0111\u01a1n gi\u1ea3n nh\u1ea5t c\u00f3 th\u1ec3.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Node.js, m\u00f4 h\u00ecnh h\u01b0\u1edbng s\u1ef1 ki\u1ec7n cho ph\u00e9p l\u1eadp tr\u00ecnh vi\u00ean vi\u1ebft m\u00e3 kh\u00f4ng ch\u1eb7n (non-blocking), kh\u00f4ng \u0111\u1ed3ng b\u1ed9 (asynchronous), \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c s\u1ef1 ki\u1ec7n khi ch\u00fang x\u1ea3y ra, m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i \u0111\u1ee3i c\u00e1c ho\u1ea1t \u0111\u1ed9ng ch\u1eb7n ho\u00e0n t\u1ea5t. \u0110i\u1ec1u n\u00e0y gi\u00fap \u1ee9ng d\u1ee5ng x\u1eed l\u00fd nhi\u1ec1u t\u00e1c v\u1ee5 c\u00f9ng l\u00fac m\u00e0 kh\u00f4ng b\u1ecb gi\u00e1n \u0111o\u1ea1n.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-s\u1eed-d\u1ee5ng-cac-cong-c\u1ee5-ki\u1ec3m-th\u1eed-trong-node-js-nh\u01b0-mocha-jest\"><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 s\u1eed d\u1ee5ng c\u00e1c c\u00f4ng c\u1ee5 ki\u1ec3m th\u1eed trong Node.js (nh\u01b0 Mocha, Jest)?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Ki\u1ec3m th\u1eed \u0111\u01a1n v\u1ecb (unit testing) l\u00e0 vi\u1ec7c c\u00f4 l\u1eadp c\u00e1c th\u00e0nh ph\u1ea7n ho\u1eb7c \u0111\u01a1n v\u1ecb m\u00e3, ch\u1eb3ng h\u1ea1n nh\u01b0 h\u00e0m ho\u1eb7c l\u1edbp, v\u00e0 chuy\u1ec3n ch\u00fang \u0111\u1ebfn c\u00e1c tr\u01b0\u1eddng h\u1ee3p th\u1eed nghi\u1ec7m kh\u00e1c nhau \u0111\u1ec3 x\u00e1c minh h\u00e0nh vi c\u1ee7a ch\u00fang.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\"><strong>Jest<\/strong> l\u00e0 m\u1ed9t framework ki\u1ec3m th\u1eed JavaScript \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng r\u1ed9ng r\u00e3i \u0111\u1ec3 ki\u1ec3m th\u1eed c\u00e1c \u1ee9ng d\u1ee5ng React v\u00e0 c\u01a1 s\u1edf m\u00e3 (codebase) JavaScript b\u1ea5t k\u1ef3.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">test('m\u00f4 t\u1ea3 test case', () =&gt; {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;\/\/ Code d\u00f9ng \u0111\u1ec3 test \u1edf \u0111\u00e2y<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;expect(\/* value th\u1eadt *\/).toBe(\/* value d\u1ef1 ki\u1ebfn *\/);<\/span>\n\n<span style=\"font-weight: 400;\">});<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\"><strong>Mocha<\/strong> l\u00e0 m\u1ed9t Framework ki\u1ec3m th\u1eed JavaScript linh ho\u1ea1t, ch\u1ea1y tr\u00ean Node.js v\u00e0 trong tr\u00ecnh duy\u1ec7t. N\u00f3 cung c\u1ea5p m\u1ed9t m\u00f4i tr\u01b0\u1eddng ki\u1ec3m th\u1eed t\u1ed1i gi\u1ea3n, cho ph\u00e9p l\u1ef1a ch\u1ecdn c\u00e1c th\u01b0 vi\u1ec7n kh\u1eb3ng \u0111\u1ecbnh (assertion libraries) v\u00e0 framework m\u00f4 ph\u1ecfng (mocking frameworks).<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">describe('m\u00f4 t\u1ea3 suite', () =&gt; {<\/span>\n\n<span style=\"font-weight: 400;\"> &nbsp;it('m\u00f4 t\u1ea3 test case', () =&gt; {<\/span>\n\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;\/\/ Code d\u00f9ng \u0111\u1ec3 test \u1edf \u0111\u00e2y<\/span>\n\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;assert.equal(\/* value th\u1eadt *\/, \/* value d\u1ef1 ki\u1ebfn *\/);<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;});<\/span>\n\n<span style=\"font-weight: 400;\">});<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-gi\u1ea3i-thich-cach-s\u1eed-d\u1ee5ng-module-timer-trong-node-js\"><b>Gi\u1ea3i th\u00edch c\u00e1ch s\u1eed d\u1ee5ng module timer trong Node.js<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Module Timers trong Node.js ch\u1ee9a nhi\u1ec1u h\u00e0m kh\u00e1c nhau cho ph\u00e9p l\u1eadp tr\u00ecnh vi\u00ean th\u1ef1c thi m\u1ed9t kh\u1ed1i m\u00e3 ho\u1eb7c m\u1ed9t h\u00e0m sau m\u1ed9t kho\u1ea3ng th\u1eddi gian nh\u1ea5t \u0111\u1ecbnh. Module Timers l\u00e0 module to\u00e0n c\u1ee5c, l\u1eadp tr\u00ecnh vi\u00ean kh\u00f4ng c\u1ea7n s\u1eed d\u1ee5ng require() \u0111\u1ec3 import module n\u00e0y.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 s\u1eed d\u1ee5ng module timer trong Node.js b\u1eb1ng nh\u1eefng c\u00e1ch sau:<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-ph\u01b0\u01a1ng-th\u1ee9c-settimeout\"><b>Ph\u01b0\u01a1ng th\u1ee9c setTimeout()<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">H\u00e0m setTimeout kh\u00f4ng ch\u1eb7n m\u00e3 kh\u00e1c v\u00e0 ph\u1ea7n c\u00f2n l\u1ea1i c\u1ee7a m\u00e3 \u0111\u01b0\u1ee3c th\u1ef1c thi sau m\u1ed9t kho\u1ea3ng th\u1eddi gian ch\u1ec9 \u0111\u1ecbnh.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:&nbsp;<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">setTimeout(function() {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;console.log('Th\u1ef1c thi sau delay');<\/span>\n\n<span style=\"font-weight: 400;\">}, 1000);<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-ph\u01b0\u01a1ng-th\u1ee9c-setimmediate\"><b>Ph\u01b0\u01a1ng th\u1ee9c setImmediate()<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u1edbp Immediate gi\u1eef v\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n ho\u1ea1t \u0111\u1ed9ng cho \u0111\u1ebfn khi tr\u1ea3 v\u1ec1 \u2018True\u2019 n\u1ebfu tr\u1ea3 v\u1ec1 \u2018false\u2019 s\u1ebd ph\u00e1 v\u1ee1 v\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:&nbsp;<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">setImmediate(() =&gt; {<\/span>\n\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('Th\u1ef1c thi \u1edf l\u1ea7n l\u1eb7p ti\u1ebfp theo c\u1ee7a event loop');<\/span>\n\n<span style=\"font-weight: 400;\">});<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-ph\u01b0\u01a1ng-th\u1ee9c-setinterval\"><b>Ph\u01b0\u01a1ng th\u1ee9c setInterval()<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">H\u00e0m n\u00e0y cho ph\u00e9p th\u1ef1c hi\u1ec7n m\u1ed9t c\u00f4ng vi\u1ec7c l\u1eb7p l\u1ea1i sau m\u1ed9t kho\u1ea3ng th\u1eddi gian c\u1ed1 \u0111\u1ecbnh. N\u00f3 tr\u1ea3 v\u1ec1 m\u1ed9t ID duy nh\u1ea5t, m\u00e0 b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng v\u1edbi ph\u01b0\u01a1ng th\u1ee9c clearInterval() \u0111\u1ec3 d\u1eebng vi\u1ec7c l\u1eb7p l\u1ea1i.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:&nbsp;<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">const intervalId = setInterval(() =&gt; {<\/span>\n\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('Th\u1ef1c thi m\u1ed7i 2 gi\u00e2y');<\/span>\n\n<span style=\"font-weight: 400;\">}, 2000);<\/span>\n\n<span style=\"font-weight: 400;\">clearInterval(intervalId);<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-gi\u1ea3i-thich-ng\u1eafn-g\u1ecdn-s\u1ef1-khac-bi\u1ec7t-gi\u1eefa-ph\u01b0\u01a1ng-th\u1ee9c-process-nexttick-va-setimmediate\"><b>Gi\u1ea3i th\u00edch ng\u1eafn g\u1ecdn s\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa ph\u01b0\u01a1ng th\u1ee9c process.nextTick() v\u00e0 setImmediate()<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c <strong>process.nextTick()<\/strong> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 th\u00eam m\u1ed9t h\u00e0m callback m\u1edbi v\u00e0o \u0111\u1ea7u h\u00e0ng \u0111\u1ee3i s\u1ef1 ki\u1ec7n ti\u1ebfp theo. N\u00f3 \u0111\u01b0\u1ee3c g\u1ecdi tr\u01b0\u1edbc khi s\u1ef1 ki\u1ec7n \u0111\u01b0\u1ee3c x\u1eed l\u00fd.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c <strong>setImmediate<\/strong> \u0111\u01b0\u1ee3c g\u1ecdi \u1edf giai \u0111o\u1ea1n ki\u1ec3m tra c\u1ee7a h\u00e0ng \u0111\u1ee3i s\u1ef1 ki\u1ec7n ti\u1ebfp theo. N\u00f3 \u0111\u01b0\u1ee3c t\u1ea1o trong giai \u0111o\u1ea1n th\u0103m d\u00f2 v\u00e0 \u0111\u01b0\u1ee3c g\u1ecdi trong giai \u0111o\u1ea1n ki\u1ec3m tra.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-s\u1ef1-khac-bi\u1ec7t-gi\u1eefa-ph\u01b0\u01a1ng-th\u1ee9c-spawn-va-fork-la-gi\"><b>S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa ph\u01b0\u01a1ng th\u1ee9c spawn() v\u00e0 fork() l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c <strong>spawn()<\/strong> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 kh\u1edfi ch\u1ea1y m\u1ed9t ti\u1ebfn tr\u00ecnh m\u1edbi b\u1eb1ng m\u1ed9t l\u1ec7nh nh\u1ea5t \u0111\u1ecbnh.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong khi \u0111\u00f3, ph\u01b0\u01a1ng th\u1ee9c <strong>fork()<\/strong> l\u00e0 phi\u00ean b\u1ea3n chuy\u00ean bi\u1ec7t c\u1ee7a spawn() \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf ri\u00eang \u0111\u1ec3 t\u1ea1o ra c\u00e1c ti\u1ebfn tr\u00ecnh Node.js m\u1edbi.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa ph\u01b0\u01a1ng th\u1ee9c spawn() v\u00e0 fork() \u0111\u01b0\u1ee3c h\u1ec7 th\u1ed1ng qua c\u00e1c ti\u00eau ch\u00ed sau:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>&nbsp;<\/td><td><b>spawn()<\/b><\/td><td><b>fork()<\/b><\/td><\/tr><tr><td><b>C\u00fa ph\u00e1p<\/b><\/td><td><span style=\"font-weight: 400;\">const child = spawn(command, [args], [options]);<\/span><\/td><td><span style=\"font-weight: 400;\">const child = fork(modulePath, [args], [options]);<\/span><\/td><\/tr><tr><td><b>M\u1ee5c \u0111\u00edch<\/b><\/td><td><span style=\"font-weight: 400;\">\u0110a n\u0103ng.&nbsp;<\/span>\n<p><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 kh\u1edfi ch\u1ea1y b\u1ea5t k\u1ef3 quy tr\u00ecnh m\u1edbi n\u00e0o, ph\u00f9 h\u1ee3p \u0111\u1ec3 th\u1ef1c thi l\u1ec7nh shell v\u00e0 t\u01b0\u01a1ng t\u00e1c v\u1edbi lu\u1ed3ng c\u1ee7a ch\u00fang.<\/span><\/p>\n<\/td><td><span style=\"font-weight: 400;\">Chuy\u00ean bi\u1ec7t.<\/span>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 t\u1ea1o quy tr\u00ecnh Node.js m\u1edbi, \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u h\u00f3a \u0111\u1ec3 giao ti\u1ebfp gi\u1eefa quy tr\u00ecnh cha v\u00e0 con th\u00f4ng qua IPC.<\/span><\/p>\n<\/td><\/tr><tr><td><b>Tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng<\/b><\/td><td><span style=\"font-weight: 400;\">Ch\u1ea1y l\u1ec7nh b\u00ean ngo\u00e0i, t\u1eadp l\u1ec7nh shell ho\u1eb7c b\u1ea5t k\u1ef3 quy tr\u00ecnh n\u00e0o c\u1ea7n truy\u1ec1n ph\u00e1t d\u1eef li\u1ec7u theo th\u1eddi gian th\u1ef1c.<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ea1y c\u00e1c t\u1eadp l\u1ec7nh Node.js ri\u00eang bi\u1ec7t c\u1ea7n giao ti\u1ebfp v\u1edbi quy tr\u00ecnh cha, ch\u1eb3ng h\u1ea1n nh\u01b0 ph\u00e2n nh\u00e1nh nhi\u1ec1u phi\u00ean b\u1ea3n c\u1ee7a \u1ee9ng d\u1ee5ng Node.js.<\/span><\/td><\/tr><tr><td><b>Giao ti\u1ebfp<\/b><\/td><td><span style=\"font-weight: 400;\">Gi\u1edbi h\u1ea1n \u1edf c\u00e1c lu\u1ed3ng \u0111\u1ea7u v\u00e0o\/\u0111\u1ea7u ra ti\u00eau chu\u1ea9n.<\/span><\/td><td><span style=\"font-weight: 400;\">Cung c\u1ea5p k\u00eanh IPC \u0111\u1ec3 truy\u1ec1n th\u00f4ng \u0111i\u1ec7p gi\u1eefa c\u00e1c quy tr\u00ecnh cha v\u00e0 con.<\/span><\/td><\/tr><tr><td><b>Hi\u1ec7u su\u1ea5t<\/b><\/td><td><span style=\"font-weight: 400;\">T\u1ed1t h\u01a1n cho c\u00e1c t\u00e1c v\u1ee5 li\u00ean k\u1ebft I\/O n\u1eb7ng do kh\u1ea3 n\u0103ng truy\u1ec1n ph\u00e1t theo th\u1eddi gian th\u1ef1c.<\/span><\/td><td><span style=\"font-weight: 400;\">T\u1ed1t h\u01a1n cho c\u00e1c t\u00e1c v\u1ee5 y\u00eau c\u1ea7u ph\u1ed1i h\u1ee3p ch\u1eb7t ch\u1ebd gi\u1eefa c\u00e1c quy tr\u00ecnh Node.js, ch\u1eb3ng h\u1ea1n nh\u01b0 qu\u1ea3n l\u00fd tr\u1ea1ng th\u00e1i \u0111\u01b0\u1ee3c chia s\u1ebb ho\u1eb7c giao ti\u1ebfp gi\u1eefa c\u00e1c quy tr\u00ecnh ph\u1ee9c t\u1ea1p.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-k\u1ebft-n\u1ed1i-node-js-v\u1edbi-c\u01a1-s\u1edf-d\u1eef-li\u1ec7u-mongodb\"><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 k\u1ebft n\u1ed1i Node.js v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u MongoDB?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">MongoDB l\u00e0 m\u1ed9t c\u01a1 s\u1edf d\u1eef li\u1ec7u NoSQL \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u01b0u tr\u1eef l\u01b0\u1ee3ng l\u1edbn d\u1eef li\u1ec7u m\u00e0 kh\u00f4ng c\u1ea7n b\u1ea5t k\u1ef3 b\u1ea3ng c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7 truy\u1ec1n th\u1ed1ng n\u00e0o.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 k\u1ebft n\u1ed1i Node v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u MongoDB, l\u1eadp tr\u00ecnh vi\u00ean c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n mongoose t\u1eeb npm.<\/span><\/p>\n\n\n\n<p><b>B\u01b0\u1edbc 1:<\/b><span style=\"font-weight: 400;\"> C\u00e0i \u0111\u1eb7t mongoose v\u00e0o h\u1ec7 th\u1ed1ng b\u1eb1ng l\u1ec7nh:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">npm install mongoose<\/span><\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 2:<\/b><span style=\"font-weight: 400;\"> S\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n Mongoose \u0111\u1ec3 nh\u1eadp th\u01b0 vi\u1ec7n:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">const mongoose = require (\"mongoose\");<\/span><\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 3:<\/b><span style=\"font-weight: 400;\"> G\u1ecdi ph\u01b0\u01a1ng th\u1ee9c k\u1ebft n\u1ed1i c\u1ee7a Mongoose<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">mongoose.connect(\"mongodb:\/\/localhost:27017\/collectionName\", {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;useNewUrlParser: true,<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;useUnifiedTopology: true<\/span>\n\n<span style=\"font-weight: 400;\">});<\/span><\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 4: <\/b><span style=\"font-weight: 400;\">X\u00e1c \u0111\u1ecbnh l\u01b0\u1ee3c \u0111\u1ed3.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: Gi\u1ea3 s\u1eed ch\u00fang ta mu\u1ed1n l\u01b0u tr\u1eef th\u00f4ng tin t\u1eeb bi\u1ec3u m\u1eabu li\u00ean h\u1ec7 c\u1ee7a m\u1ed9t trang web.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">&nbsp;const contactSchema = {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;email: String,<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;query: String,<\/span>\n\n<span style=\"font-weight: 400;\">};<\/span><\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 5: <\/b><span style=\"font-weight: 400;\">T\u1ea1o m\u00f4 h\u00ecnh v\u1edbi l\u01b0\u1ee3c \u0111\u1ed3 \u0111\u00e3 x\u00e1c \u0111\u1ecbnh v\u1edbi c\u00e2u l\u1ec7nh:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">const <\/span><span style=\"font-weight: 400;\">Contact = mongoose.model(\"Contact\", contactSchema);<\/span><\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 6:<\/b><span style=\"font-weight: 400;\"> L\u01b0u tr\u1eef d\u1eef li\u1ec7u&nbsp;<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">app.post(\"\/contact\", function (req, res) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;const contact = new Contact({<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email: req.body.email,<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query: req.body.query,<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;});<\/span>\n\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;contact.save(function (err) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (err) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res.redirect(\"\/error\");<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res.redirect(\"\/thank-you\");<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;});<\/span>\n<span style=\"font-weight: 400;\">});<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-x\u1eed-ly-routing-trong-\u1ee9ng-d\u1ee5ng-node-js\"><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 x\u1eed l\u00fd routing trong \u1ee9ng d\u1ee5ng Node.js?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ecbnh tuy\u1ebfn (routing) trong Node x\u00e1c \u0111\u1ecbnh c\u00e1ch c\u00e1c y\u00eau c\u1ea7u c\u1ee7a ng\u01b0\u1eddi d\u00f9ng \u0111\u01b0\u1ee3c x\u1eed l\u00fd b\u1edfi c\u00e1c \u0111i\u1ec3m cu\u1ed1i (endpoint) kh\u00e1c nhau (URL). C\u00f3 hai c\u00e1ch \u0111\u1ec3 tri\u1ec3n khai \u0111\u1ecbnh tuy\u1ebfn trong Node nh\u01b0 sau:<\/span><\/p>\n\n\n\n<p><b>C\u00e1ch 1: <\/b><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng Framework Express v\u00e0 th\u1ef1c hi\u1ec7n theo c\u00e1c b\u01b0\u1edbc sau:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">B\u01b0\u1edbc 1: C\u00e0i \u0111\u1eb7t Express.js v\u1edbi l\u1ec7nh npm install express<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">B\u01b0\u1edbc 2: T\u1ea1o m\u1ed9t \u1ee9ng d\u1ee5ng Express \u0111\u01a1n gi\u1ea3n<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">B\u01b0\u1edbc 3: S\u1eed d\u1ee5ng c\u00e1c tham s\u1ed1 \u0111\u1ed9ng trong \u0111\u01b0\u1eddng d\u1eabn URL \u0111\u1ec3 l\u1ea5y th\u00f4ng tin t\u1eeb URL<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>C\u00e1ch 2:<\/b><span style=\"font-weight: 400;\"> X\u1eed l\u00fd thu\u1ea7n t\u00fay b\u1eb1ng ph\u01b0\u01a1ng th\u1ee9c app.all()<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">const express = require('express')<\/span>\n<span style=\"font-weight: 400;\">const app = express()<\/span>\n<span style=\"font-weight: 400;\">app.all('\/', function(req, res) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('Hello Sir')<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;next() &nbsp; \/\/ Pass the control to the next handler<\/span>\n<span style=\"font-weight: 400;\">})<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-body-paser-trong-node-js-la-gi\"><b>Body-paser trong Node.js l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Body-parser l\u00e0 m\u1ed9t ph\u1ea7n m\u1ec1m trung gian gi\u00fap ph\u00e2n t\u00edch c\u00fa ph\u00e1p (parse) d\u1eef li\u1ec7u trong body c\u1ee7a y\u00eau c\u1ea7u HTTP \u0111\u1ebfn trong Node.js. N\u00f3 ch\u1ecbu tr\u00e1ch nhi\u1ec7m x\u1eed l\u00fd d\u1eef li\u1ec7u body c\u1ee7a y\u00eau c\u1ea7u tr\u01b0\u1edbc khi th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c v\u1edbi ch\u00fang.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Ph\u1ea7n m\u1ec1m n\u00e0y th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c \u1ee9ng d\u1ee5ng web x\u00e2y d\u1ef1ng b\u1eb1ng Express.js \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u t\u1eeb c\u00e1c form g\u1eedi \u0111i, JSON payload v\u00e0 c\u00e1c lo\u1ea1i body y\u00eau c\u1ea7u kh\u00e1c.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-n-api-trong-node-js-la-gi\"><b>N-API trong Node.js l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">N-API l\u00e0 m\u1ed9t API gi\u00fap x\u00e2y d\u1ef1ng c\u00e1c Addon g\u1ed1c cho Node.js. API n\u00e0y ho\u1ea1t \u0111\u1ed9ng \u0111\u1ed9c l\u1eadp v\u1edbi m\u00f4i tr\u01b0\u1eddng ch\u1ea1y JavaScript v\u00e0 l\u00e0 m\u1ed9t ph\u1ea7n t\u00edch h\u1ee3p c\u1ee7a Node.js.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u1edbi thi\u1ebft k\u1ebf \u1ed5n \u0111\u1ecbnh d\u1ef1a tr\u00ean Application Binary Interface (ABI), N-API gi\u00fap c\u00e1c Addon kh\u00f4ng b\u1ecb \u1ea3nh h\u01b0\u1edfng b\u1edfi nh\u1eefng thay \u0111\u1ed5i trong c\u00f4ng c\u1ee5 JavaScript. Nh\u1edd \u0111\u00f3, c\u00e1c m\u00f4-\u0111un \u0111\u01b0\u1ee3c bi\u00ean d\u1ecbch m\u1ed9t l\u1ea7n c\u00f3 th\u1ec3 ch\u1ea1y tr\u00ean nhi\u1ec1u phi\u00ean b\u1ea3n Node.js sau n\u00e0y m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i bi\u00ean d\u1ecbch l\u1ea1i.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-non-block-trong-node-js-la-gi\"><b>Non-block trong Node.js l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Node.js, non-blocking \u0111\u1ec1 c\u1eadp \u0111\u1ebfn kh\u1ea3 n\u0103ng c\u1ee7a m\u00f4i tr\u01b0\u1eddng th\u1eddi gian ch\u1ea1y \u0111\u1ec3 th\u1ef1c hi\u1ec7n nhi\u1ec1u t\u00e1c v\u1ee5 c\u00f9ng l\u00fac m\u00e0 kh\u00f4ng c\u1ea7n ch\u1edd m\u1ed9t t\u00e1c v\u1ee5 ho\u00e0n th\u00e0nh tr\u01b0\u1edbc khi b\u1eaft \u0111\u1ea7u t\u00e1c v\u1ee5 ti\u1ebfp theo. \u0110i\u1ec1u n\u00e0y \u0111\u1ea1t \u0111\u01b0\u1ee3c th\u00f4ng qua vi\u1ec7c s\u1eed d\u1ee5ng c\u00e1c ho\u1ea1t \u0111\u1ed9ng I\/O kh\u00f4ng \u0111\u1ed3ng b\u1ed9, cho ph\u00e9p Node.js x\u1eed l\u00fd nhi\u1ec1u y\u00eau c\u1ea7u \u0111\u1ed3ng th\u1eddi.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-qu\u1ea3n-ly-session-trong-node-js\"><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 qu\u1ea3n l\u00fd session trong Node.js?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Qu\u1ea3n l\u00fd phi\u00ean c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n trong node.js b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng m\u00f4-\u0111un express-session.&nbsp;<\/span><\/p>\n\n\n\n<p><b>B\u01b0\u1edbc 1:<\/b><span style=\"font-weight: 400;\"> C\u00e0i \u0111\u1eb7t m\u00f4-\u0111un express-session b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng l\u1ec7nh <\/span><span style=\"font-weight: 400;\">npm install express-session<\/span><\/p>\n\n\n\n<p><b>B\u01b0\u1edbc 2<\/b><span style=\"font-weight: 400;\">: Ki\u1ec3m tra phi\u00ean b\u1ea3n l\u00e0 m\u1ed9t b\u01b0\u1edbc t\u00f9y ch\u1ecdn, kh\u00f4ng b\u1eaft bu\u1ed9c. B\u1ea1n c\u00f3 th\u1ec3 ki\u1ec3m tra phi\u00ean b\u1ea3n \u0111\u00e3 c\u00e0i b\u1eb1ng m\u1ed9t trong c\u00e1c l\u1ec7nh sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">npm list express-session<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Ho\u1eb7c:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">npm view express-session version<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\"># Ho\u1eb7c (trong package.json)<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">cat package.json | grep express-session<\/span><\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 3:<\/b><span style=\"font-weight: 400;\"> Vi\u1ebft code v\u00e0 s\u1eed d\u1ee5ng express-session v\u00e0 kh\u1edfi ch\u1ea1y ch\u00fang v\u1edbi NodeJS<\/span>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Tham kh\u1ea3o <strong><a href=\"https:\/\/itviec.com\/it-jobs\/nodejs?utm_medium=anchor_text_mid&amp;utm_source=blog&amp;utm_campaign=vec_lam_nodejs&amp;utm_content=cau_hoi_phong_van_nodejs\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m NodeJS<\/a><\/strong> m\u1edbi nh\u1ea5t tr\u00ean to\u00e0n qu\u1ed1c<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-nodejs-danh-cho-senior-developer\"><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_NodeJS_danh_cho_Senior_Developer\"><\/span><b>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS d\u00e0nh cho Senior Developer<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-hay-cho-bi\u1ebft-t\u1ea1i-sao-c\u1ea7n-co-c-trong-node-js\"><b>H\u00e3y cho bi\u1ebft t\u1ea1i sao c\u1ea7n c\u00f3 C++ trong Node.js?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Node.js Addon l\u00e0 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng chia s\u1ebb \u0111\u01b0\u1ee3c li\u00ean k\u1ebft \u0111\u1ed9ng, vi\u1ebft b\u1eb1ng C++, c\u00f3 th\u1ec3 t\u1ea3i v\u00e0o Node.js b\u1eb1ng h\u00e0m require() v\u00e0 s\u1eed d\u1ee5ng nh\u01b0 m\u1ed9t m\u00f4-\u0111un Node.js th\u00f4ng th\u01b0\u1eddng, \u0111\u1ec3 cung c\u1ea5p giao di\u1ec7n gi\u1eefa JavaScript ch\u1ea1y trong Node.js v\u00e0 c\u00e1c th\u01b0 vi\u1ec7n C\/C++.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00f3 nhi\u1ec1u l\u00fd do \u0111\u1ec3 vi\u1ebft c\u00e1c Addon trong Node.js, v\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">B\u1ea1n mu\u1ed1n truy c\u1eadp m\u1ed9t s\u1ed1 API g\u1ed1c m\u00e0 JavaScript th\u00f4ng th\u01b0\u1eddng kh\u00f4ng th\u1ec3 x\u1eed l\u00fd d\u1ec5 d\u00e0ng.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">B\u1ea1n c\u1ea7n t\u00edch h\u1ee3p m\u1ed9t th\u01b0 vi\u1ec7n c\u1ee7a b\u00ean th\u1ee9 ba vi\u1ebft b\u1eb1ng C\/C++ \u0111\u1ec3 s\u1eed d\u1ee5ng tr\u1ef1c ti\u1ebfp trong Node.js.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">B\u1ea1n mu\u1ed1n vi\u1ebft l\u1ea1i m\u1ed9t s\u1ed1 m\u00f4-\u0111un b\u1eb1ng C++ \u0111\u1ec3 c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-t\u1ea1i-sao-b\u1ea1n-nen-tach-\u1ee9ng-d\u1ee5ng-express-va-may-ch\u1ee7\"><b>T\u1ea1i sao b\u1ea1n n\u00ean t\u00e1ch \u1ee9ng d\u1ee5ng Express v\u00e0 m\u00e1y ch\u1ee7?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ea7u ti\u00ean, vi\u1ec7c t\u00e1ch \u1ee9ng d\u1ee5ng v\u00e0 m\u00e1y ch\u1ee7 c\u00f3 th\u1ec3 gi\u00fap ki\u1ec3m tra code d\u1ec5 d\u00e0ng h\u01a1n. B\u1eb1ng c\u00e1ch t\u00e1ch bi\u1ec7t hai th\u00e0nh ph\u1ea7n n\u00e0y, l\u1eadp tr\u00ecnh vi\u00ean c\u00f3 th\u1ec3 ki\u1ec3m tra logic \u1ee9ng d\u1ee5ng \u0111\u1ed9c l\u1eadp v\u1edbi m\u00e1y ch\u1ee7, t\u1eeb \u0111\u00f3 d\u1ec5 d\u00e0ng x\u00e1c \u0111\u1ecbnh v\u00e0 s\u1eeda l\u1ed7i h\u01a1n.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Th\u1ee9 hai, vi\u1ec7c t\u00e1ch bi\u1ec7t \u1ee9ng d\u1ee5ng v\u00e0 m\u00e1y ch\u1ee7 c\u00f3 th\u1ec3 gi\u00fap m\u1edf r\u1ed9ng quy m\u00f4 \u1ee9ng d\u1ee5ng h\u01a1n. Nh\u1edd \u0111\u00f3 l\u1eadp tr\u00ecnh vi\u00ean c\u00f3 th\u1ec3 ch\u1ea1y nhi\u1ec1u phi\u00ean b\u1ea3n \u1ee9ng d\u1ee5ng tr\u00ean c\u00e1c m\u00e1y ch\u1ee7 kh\u00e1c nhau, gi\u00fap ph\u00e2n b\u1ed5 t\u1ea3i v\u00e0 c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Cu\u1ed1i c\u00f9ng, vi\u1ec7c t\u00e1ch bi\u1ec7t \u1ee9ng d\u1ee5ng v\u00e0 m\u00e1y ch\u1ee7 c\u00f3 th\u1ec3 gi\u00fap d\u1ec5 d\u00e0ng chuy\u1ec3n sang m\u00e1y ch\u1ee7 kh\u00e1c, m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i th\u1ef1c hi\u1ec7n b\u1ea5t k\u1ef3 thay \u0111\u1ed5i l\u1edbn n\u00e0o \u0111\u1ed1i v\u1edbi code.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-vi\u1ebft-l\u1ea1i-cac-\u1ee9ng-d\u1ee5ng-node-js-d\u1ef1a-tren-promise-thanh-async-await\"><b>Vi\u1ebft l\u1ea1i c\u00e1c \u1ee9ng d\u1ee5ng Node.js d\u1ef1a tr\u00ean promise th\u00e0nh Async\/Await<\/b><\/h3>\n\n\n\n<p><b>\u0110\u1ec1 b\u00e0i:<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">function asyncTask() {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;return functionA()<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.then((valueA) =&gt; functionB(valueA))<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.then((valueB) =&gt; functionC(valueB))<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.then((valueC) =&gt; functionD(valueC))<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.catch((err) =&gt; logger.error(err))<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/code><\/pre>\n\n\n\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">async function asyncTask() {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;try {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const valueA = await functionA();<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const valueB = await functionB(valueA);<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const valueC = await functionC(valueB);<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return await functionD(valueC); \/\/ Ho\u1eb7c return functionD(valueC);<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;} catch (err) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.error(err);<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-gi\u1ea3i-thich-khai-ni\u1ec7m-domain-trong-node-js\"><b>Gi\u1ea3i th\u00edch kh\u00e1i ni\u1ec7m Domain trong Node.js?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Domains cung c\u1ea5p c\u00e1ch \u0111\u1ec3 qu\u1ea3n l\u00fd nhi\u1ec1u ho\u1ea1t \u0111\u1ed9ng I\/O kh\u00e1c nhau. N\u1ebfu m\u1ed9t event emitter ho\u1eb7c m\u1ed9t callback n\u00e0o \u0111\u00f3 \u0111\u01b0\u1ee3c \u0111\u0103ng k\u00fd trong domain ph\u00e1t sinh l\u1ed7i (emit m\u1ed9t s\u1ef1 ki\u1ec7n l\u1ed7i ho\u1eb7c qu\u0103ng ra l\u1ed7i), th\u00ec \u0111\u1ed1i t\u01b0\u1ee3ng domain s\u1ebd nh\u1eadn bi\u1ebft l\u1ed7i n\u00e0y.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110i\u1ec1u n\u00e0y gi\u00fap tr\u00e1nh m\u1ea5t ng\u1eef c\u1ea3nh l\u1ed7i khi s\u1eed d\u1ee5ng process.on(&#8216;uncaughtException&#8217;) ho\u1eb7c ng\u0103n ch\u01b0\u01a1ng tr\u00ecnh b\u1ecb tho\u00e1t ngay l\u1eadp t\u1ee9c v\u1edbi m\u00e3 l\u1ed7i.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u01b0u \u00fd: Kh\u00e1i ni\u1ec7m n\u00e0y \u0111\u00e3 b\u1ecb lo\u1ea1i b\u1ecf t\u1eeb Node.js v16<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-chuy\u1ec3n-d\u1ed5i-m\u1ed9t-callback-api-hi\u1ec7n-co-thanh-promise\"><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 chuy\u1ec3n \u0111\u1ed5i m\u1ed9t callback API hi\u1ec7n c\u00f3 th\u00e0nh promise?<\/b><\/h3>\n\n\n\n<p><b>\u0110\u1ec1 b\u00e0i<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">function divisionAPI (number, divider, successCallback, errorCallback) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;if (divider == 0) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return errorCallback( new Error(\"Division by zero\") )<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;successCallback( number \/ divider )<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/code><\/pre>\n\n\n\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">function divisionAPI(number, divider) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;return new Promise(function(fulfilled, rejected) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (divider == 0) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return rejected(new Error(\"Division by zero\"))<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fulfilled(number \/ divider)<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;})<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ Promise c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c d\u00f9ng chung v\u1edbi async\\await trong ES7 \u0111\u1ec3 cho program flow ph\u1ea3i await k\u1ebft qu\u1ea3 fulfilled<\/span>\n\n<span style=\"font-weight: 400;\">async function foo() {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;var result = await divisionAPI(1, 2); \/\/ await k\u1ebft qu\u1ea3 fulfilled!<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log(result);<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ M\u1ed9t c\u00e1ch d\u00f9ng kh\u00e1c v\u1edbi c\u00f9ng code m\u00e0 s\u1eed d\u1ee5ng .then() method\n<\/span>\n<span style=\"font-weight: 400;\">divisionAPI(1, 2).then(function(result) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log(result)<\/span>\n<span style=\"font-weight: 400;\">})<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-gi\u1ea3i-thich-m\u1ed9t-s\u1ed1-cach-x\u1eed-ly-l\u1ed7i-trong-node-js-ma-b\u1ea1n-bi\u1ebft\"><b>Gi\u1ea3i th\u00edch m\u1ed9t s\u1ed1 c\u00e1ch x\u1eed l\u00fd l\u1ed7i trong Node.js m\u00e0 b\u1ea1n bi\u1ebft<\/b><i><\/i><\/h3>\n\n\n\n<p><i><span style=\"font-weight: 400;\">&nbsp;<\/span><\/i><span style=\"font-weight: 400;\">C\u00f3 b\u1ed1n c\u00e1ch x\u1eed l\u00fd l\u1ed7i ch\u00ednh trong node:<\/span><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1 l\u1ed7i (Error return value) &#8211; kh\u00f4ng ho\u1ea1t \u0111\u1ed9ng \u0111\u1ed3ng b\u1ed9.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Qu\u0103ng l\u1ed7i (Error throwing) l\u00e0 m\u1ed9t m\u1eabu thi\u1ebft k\u1ebf ph\u1ed5 bi\u1ebfn, trong \u0111\u00f3 m\u1ed9t h\u00e0m th\u1ef1c hi\u1ec7n nhi\u1ec7m v\u1ee5 c\u1ee7a n\u00f3 v\u00e0 khi g\u1eb7p l\u1ed7i, n\u00f3 ngay l\u1eadp t\u1ee9c d\u1eebng l\u1ea1i b\u1eb1ng c\u00e1ch qu\u0103ng ra m\u1ed9t l\u1ed7i.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">G\u1ecdi l\u1ea1i l\u1ed7i (Error callback) tr\u1ea3 v\u1ec1 l\u1ed7i th\u00f4ng qua l\u1ec7nh callback l\u00e0 m\u00f4 h\u00ecnh x\u1eed l\u00fd l\u1ed7i ph\u1ed5 bi\u1ebfn nh\u1ea5t trong Node.js.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Ph\u00e1t ra l\u1ed7i (Error emitting) l\u00e0 c\u01a1 ch\u1ebf g\u1eedi l\u1ed7i \u0111\u1ebfn c\u00e1c th\u00e0nh ph\u1ea7n ho\u1eb7c h\u00e0m \u0111\u00e3 \u0111\u0103ng k\u00fd l\u1eafng nghe (thu\u00ea bao). Khi l\u1ed7i \u0111\u01b0\u1ee3c ph\u00e1t ra, n\u00f3 s\u1ebd \u0111\u01b0\u1ee3c x\u1eed l\u00fd ngay l\u1eadp t\u1ee9c trong c\u00f9ng m\u1ed9t chu k\u1ef3 c\u1ee7a v\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n, theo th\u1ee9 t\u1ef1 m\u00e0 c\u00e1c thu\u00ea bao \u0111\u01b0\u1ee3c \u0111\u0103ng k\u00fd.<\/span><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-d\u1ecdc-d\u1ed1i-s\u1ed1-trong-node-js\"><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 \u0111\u1ecdc \u0111\u1ed1i s\u1ed1 trong Node.js?<\/b><i><\/i><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ed1i s\u1ed1 d\u00f2ng l\u1ec7nh (CLI) l\u00e0 chu\u1ed7i v\u0103n b\u1ea3n \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 truy\u1ec1n th\u00f4ng tin b\u1ed5 sung cho ch\u01b0\u01a1ng tr\u00ecnh khi \u1ee9ng d\u1ee5ng \u0111ang ch\u1ea1y qua giao di\u1ec7n d\u00f2ng l\u1ec7nh c\u1ee7a h\u1ec7 \u0111i\u1ec1u h\u00e0nh. L\u1eadp tr\u00ecnh vi\u00ean c\u00f3 th\u1ec3 d\u1ec5 d\u00e0ng \u0111\u1ecdc c\u00e1c \u0111\u1ed1i s\u1ed1 n\u00e0y b\u1eb1ng \u0111\u1ed1i t\u01b0\u1ee3ng to\u00e0n c\u1ee5c trong n\u00fat t\u1ee9c l\u00e0 \u0111\u1ed1i t\u01b0\u1ee3ng quy tr\u00ecnh.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00e1c b\u01b0\u1edbc \u0111\u1ecdc \u0111\u1ed1i s\u1ed1 trong Node.js nh\u01b0 sau:<\/span><\/p>\n\n\n\n<p><b>B\u01b0\u1edbc 1: <\/b><span style=\"font-weight: 400;\">L\u01b0u t\u1ec7p d\u01b0\u1edbi d\u1ea1ng index.js v\u00e0 d\u00e1n m\u00e3 b\u00ean d\u01b0\u1edbi v\u00e0o t\u1ec7p.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">let arguments = process.argv ;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">console.log(arguments) ;<\/span><\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 2: <\/b><span style=\"font-weight: 400;\">Ch\u1ea1y t\u1ec7p index.js b\u1eb1ng l\u1ec7nh <\/span><span style=\"font-weight: 400;\">node index.js<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-hay-vi\u1ebft-l\u1ea1i-m\u1eabu-code-ben-d\u01b0\u1edbi-ma-khong-s\u1eed-d\u1ee5ng-kh\u1ed1i-catch-try\"><b>H\u00e3y vi\u1ebft l\u1ea1i m\u1eabu code b\u00ean d\u01b0\u1edbi m\u00e0 kh\u00f4ng s\u1eed d\u1ee5ng kh\u1ed1i catch\/try<\/b><\/h3>\n\n\n\n<p><b>\u0110\u1ec1 b\u00e0i:<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">async function check(req, res) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;try {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;const a = await someOtherFunction();<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;const b = await somethingElseFunction();<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;res.send(\"result\")<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;} catch (error) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;res.send(error.stack);<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/code><\/pre>\n\n\n\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">async function getData(){<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;const a = await someFunction().catch((error)=&gt;console.log(error));<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;const b = await someOtherFunction().catch((error)=&gt;console.log(error));<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;if (a &amp;&amp; b) console.log(\"some result\")<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Ho\u1eb7c n\u1ebfu b\u1ea1n mu\u1ed1n bi\u1ebft ch\u1ee9c n\u0103ng c\u1ee5 th\u1ec3 n\u00e0o g\u00e2y ra l\u1ed7i c\u00f3 th\u1ec3 vi\u1ebft l\u1ea1i nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">async function loginController() {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;try {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;const a = await loginService().<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;catch((error) =&gt; {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new CustomErrorHandler({<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;code: 101,<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message: \"a failed\",<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error: error<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;});<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;const b = await someUtil().<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;catch((error) =&gt; {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new CustomErrorHandler({<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;code: 102,<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message: \"b failed\",<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error: error<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;});<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;\/\/someoeeoe<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;if (a &amp;&amp; b) console.log(\"no one failed\")<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;} catch (error) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;if (!(error instanceof CustomErrorHandler)) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(\"gen error\", error)<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-libuv-ho\u1ea1t-d\u1ed9ng-nh\u01b0-th\u1ebf-nao\"><b>Libuv ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Libuv cung c\u1ea5p m\u1ed9t v\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n (event loop), trong \u0111\u00f3 ch\u1ec9 c\u00f3 m\u1ed9t lu\u1ed3ng duy nh\u1ea5t th\u1ef1c thi m\u00e3 JavaScript. T\u1ea5t c\u1ea3 c\u00e1c callback (m\u00e3 ng\u01b0\u1eddi d\u00f9ng trong \u1ee9ng d\u1ee5ng Node.js) \u0111\u1ec1u \u0111\u01b0\u1ee3c th\u1ef1c thi th\u00f4ng qua v\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n n\u00e0y.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">M\u1eb7c \u0111\u1ecbnh, libuv t\u1ea1o ra m\u1ed9t thread pool g\u1ed3m b\u1ed1n lu\u1ed3ng \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c c\u00f4ng vi\u1ec7c b\u1ea5t \u0111\u1ed3ng b\u1ed9. Tuy nhi\u00ean, trong h\u1ea7u h\u1ebft tr\u01b0\u1eddng h\u1ee3p, libuv \u01b0u ti\u00ean s\u1eed d\u1ee5ng c\u00e1c giao di\u1ec7n b\u1ea5t \u0111\u1ed3ng b\u1ed9 do h\u1ec7 \u0111i\u1ec1u h\u00e0nh cung c\u1ea5p (nh\u01b0 AIO tr\u00ean Linux) \u0111\u1ec3 gi\u1ea3m t\u1ea3i cho thread pool.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-hay-gi\u1ea3i-thich-k\u1ebft-qu\u1ea3-khi-th\u1ef1c-thi-code-ben-d\u01b0\u1edbi\"><b>H\u00e3y gi\u1ea3i th\u00edch k\u1ebft qu\u1ea3 khi th\u1ef1c thi code b\u00ean d\u01b0\u1edbi<\/b><i><\/i><\/h3>\n\n\n\n<p><strong>\u0110\u1ec1 b\u00e0i:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">var EventEmitter = require(\"events\");<\/span>\n<span style=\"font-weight: 400;\">var crazy = new EventEmitter();<\/span>\n\n<span style=\"font-weight: 400;\">crazy.on('event1', function () {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('event1 fired!');<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;crazy.emit('event2');<\/span>\n<span style=\"font-weight: 400;\">});<\/span>\n\n<span style=\"font-weight: 400;\">crazy.on('event2', function () {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('event2 fired!');<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;crazy.emit('event3');<\/span>\n<span style=\"font-weight: 400;\">});<\/span>\n\n<span style=\"font-weight: 400;\">crazy.on('event3', function () {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('event3 fired!');<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;crazy.emit('event1');<\/span>\n<span style=\"font-weight: 400;\">});<\/span>\n\n<span style=\"font-weight: 400;\">crazy.emit('event1');<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">B\u1ea1n s\u1ebd g\u1eb7p m\u1ed9t l\u1ed7i ngo\u1ea1i l\u1ec7 th\u00f4ng b\u00e1o r\u1eb1ng &#8220;call stack \u0111\u00e3 b\u1ecb tr\u00e0n&#8221;. Nguy\u00ean nh\u00e2n l\u00e0 do m\u1ed7i l\u1ec7nh ph\u00e1t ra s\u1ebd g\u1ecdi m\u00e3 theo c\u00e1ch \u0111\u1ed3ng b\u1ed9. V\u00ec t\u1ea5t c\u1ea3 c\u00e1c l\u1ec7nh g\u1ecdi l\u1ea1i \u0111\u1ec1u \u0111\u01b0\u1ee3c th\u1ef1c thi \u0111\u1ed3ng b\u1ed9, n\u00ean ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd \u0111\u1ec7 quy ch\u00ednh n\u00f3 kh\u00f4ng ng\u1eebng, d\u1eabn \u0111\u1ebfn tr\u00e0n b\u1ed9 nh\u1edb stack.<\/span><\/p>\n\n\n\n<p><b>Output:<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">console.js:165<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0if (isStackOverflowError(e))<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0^<\/span>\n\n<span style=\"font-weight: 400;\">RangeError: Maximum call stack size exceeded<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0at write (console.js:165:9)<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0at Console.log (console.js:197:3)<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0at EventEmitter.&lt;anonymous> (C:\\Work\\Node\\main.js:6:13)<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0at EventEmitter.emit (events.js:182:13)<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0at EventEmitter.&lt;anonymous> (C:\\Work\\Node\\main.js:18:11)<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0at EventEmitter.emit (events.js:182:13)<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0at EventEmitter.&lt;anonymous> (C:\\Work\\Node\\main.js:12:11)<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0at EventEmitter.emit (events.js:182:13)<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0at EventEmitter.&lt;anonymous> (C:\\Work\\Node\\main.js:7:11)<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0at EventEmitter.emit (events.js:182:13)<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-di\u1ec1u-gi-s\u1ebd-x\u1ea3y-ra-khi-do\u1ea1n-ma-ben-d\u01b0\u1edbi-d\u01b0\u1ee3c-th\u1ef1c-thi\"><b>\u0110i\u1ec1u g\u00ec s\u1ebd x\u1ea3y ra khi \u0111o\u1ea1n m\u00e3 b\u00ean d\u01b0\u1edbi \u0111\u01b0\u1ee3c th\u1ef1c thi?<\/b><\/h3>\n\n\n\n<p><b>\u0110\u1ec1 b\u00e0i<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">var EventEmitter = require('events');<\/span>\n<span style=\"font-weight: 400;\">var crazy = new EventEmitter();\n<\/span>\n<span style=\"font-weight: 400;\">crazy.on('event1', function () {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('event1 fired!');<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;process.nextTick(function () {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crazy.emit('event2');<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;});<\/span>\n<span style=\"font-weight: 400;\">});<\/span>\n\n<span style=\"font-weight: 400;\">crazy.on('event2', function () {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('event2 fired!');<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;process.nextTick(function () {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crazy.emit('event3');<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;});<\/span>\n<span style=\"font-weight: 400;\">})<\/span>\n\n<span style=\"font-weight: 400;\">crazy.on('event3', function () {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;console.log('event3 fired!');<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;process.nextTick(function () {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crazy.emit('event1');<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;});<\/span>\n<span style=\"font-weight: 400;\">});<\/span>\n\n<span style=\"font-weight: 400;\">crazy.emit('event1');<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 th\u1ef1c t\u1ebf:<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Khi ch\u1ea1y \u0111o\u1ea1n m\u00e3, b\u1ea1n s\u1ebd th\u1ea5y output t\u01b0\u01a1ng t\u1ef1 nh\u01b0 sau (ti\u1ebfp t\u1ee5c v\u00f4 h\u1ea1n):<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">event1 fired!<\/span>\n\n<span style=\"font-weight: 400;\">event2 fired!<\/span>\n\n<span style=\"font-weight: 400;\">event3 fired!<\/span>\n\n<span style=\"font-weight: 400;\">event1 fired!<\/span>\n\n<span style=\"font-weight: 400;\">event2 fired!<\/span>\n\n<span style=\"font-weight: 400;\">event3 fired!<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-mo-dun-c\u1ee5m-cluster-module-ho\u1ea1t-d\u1ed9ng-nh\u01b0-th\u1ebf-nao-co-gi-khac-so-v\u1edbi-b\u1ed9-load-balance\"><b>M\u00f4-\u0111un c\u1ee5m (cluster module) ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o? C\u00f3 g\u00ec kh\u00e1c so v\u1edbi b\u1ed9 load balance?<\/b><i><\/i><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">M\u00f4-\u0111un Cluster trong Node.js t\u1ea1o ra c\u00e1c ti\u1ebfn tr\u00ecnh con b\u1eb1ng c\u00e1ch fork t\u1eeb m\u00e1y ch\u1ee7 ch\u00ednh. N\u00f3 h\u1ed7 tr\u1ee3 hai c\u00e1ch ph\u00e2n ph\u1ed1i k\u1ebft n\u1ed1i:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Ph\u01b0\u01a1ng ph\u00e1p lu\u00e2n phi\u00ean (round-robin):<\/b><span style=\"font-weight: 400;\"> \u0110\u00e2y l\u00e0 c\u00e1ch m\u1eb7c \u0111\u1ecbnh (tr\u1eeb tr\u00ean Windows). Quy tr\u00ecnh ch\u00ednh s\u1ebd l\u1eafng nghe tr\u00ean m\u1ed9t c\u1ed5ng, nh\u1eadn c\u00e1c k\u1ebft n\u1ed1i m\u1edbi v\u00e0 ph\u00e2n ph\u1ed1i ch\u00fang l\u1ea7n l\u01b0\u1ee3t \u0111\u1ebfn c\u00e1c ti\u1ebfn tr\u00ecnh con. Ph\u01b0\u01a1ng ph\u00e1p n\u00e0y t\u00edch h\u1ee3p m\u1ed9t s\u1ed1 c\u01a1 ch\u1ebf th\u00f4ng minh \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o kh\u00f4ng c\u00f3 ti\u1ebfn tr\u00ecnh n\u00e0o b\u1ecb qu\u00e1 t\u1ea3i.<\/span><\/li>\n\n\n\n<li><b>Ph\u01b0\u01a1ng ph\u00e1p l\u1eafng nghe tr\u1ef1c ti\u1ebfp: <\/b><span style=\"font-weight: 400;\">Quy tr\u00ecnh ch\u00ednh t\u1ea1o m\u1ed9t socket l\u1eafng nghe v\u00e0 g\u1eedi socket n\u00e0y \u0111\u1ebfn c\u00e1c ti\u1ebfn tr\u00ecnh con. C\u00e1c ti\u1ebfn tr\u00ecnh n\u00e0y s\u1ebd tr\u1ef1c ti\u1ebfp ch\u1ea5p nh\u1eadn c\u00e1c k\u1ebft n\u1ed1i \u0111\u1ebfn.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110i\u1ec3m kh\u00e1c bi\u1ec7t gi\u1eefa m\u00f4-\u0111un Cluster v\u00e0 b\u1ed9 c\u00e2n b\u1eb1ng t\u1ea3i (load balancer) l\u00e0: Cluster ph\u00e2n ph\u1ed1i t\u1ea3i gi\u1eefa c\u00e1c ti\u1ebfn tr\u00ecnh, c\u00f2n b\u1ed9 c\u00e2n b\u1eb1ng t\u1ea3i ph\u00e2n ph\u1ed1i c\u00e1c y\u00eau c\u1ea7u \u0111\u1ebfn c\u00e1c m\u00e1y ch\u1ee7 kh\u00e1c nhau.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-k\u1ebft-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-nodejs\"><span class=\"ez-toc-section\" id=\"Tong_ket_cau_hoi_phong_van_NodeJS\"><\/span><b>T\u1ed5ng k\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Nh\u01b0 v\u1eady, b\u1ea1n \u0111\u00e3 ho\u00e0n th\u00e0nh b\u1ed9 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS r\u1ed3i \u0111\u1ea5y. Nh\u1eefng b\u00e0i th\u1ef1c h\u00e0nh v\u00e0 ph\u1ea7n l\u00fd thuy\u1ebft h\u1eb3n kh\u00f4ng l\u00e0m kh\u00f3 b\u1ea1n \u0111\u00fang kh\u00f4ng n\u00e0o? Ngo\u00e0i nh\u1eefng ki\u1ebfn th\u1ee9c tr\u00ean, \u0111\u1eebng qu\u00ean ti\u1ebfp t\u1ee5c c\u1eadp nh\u1eadt nh\u1eefng ki\u1ebfn th\u1ee9c m\u1edbi v\u1ec1 Node.js v\u00e0 th\u1ef1c h\u00e0nh nhi\u1ec1u h\u01a1n nh\u00e9!<\/span><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Tham kh\u1ea3o <strong><a href=\"https:\/\/itviec.com\/it-jobs\/nodejs?utm_medium=anchor_text_low&amp;utm_source=blog&amp;utm_campaign=vec_lam_nodejs&amp;utm_content=cau_hoi_phong_van_nodejs\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m NodeJS<\/a><\/strong> m\u1edbi nh\u1ea5t tr\u00ean to\u00e0n qu\u1ed1c<\/em><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Trong b\u00e0i vi\u1ebft n\u00e0y, ITviec \u0111\u00e3 t\u1ed5ng h\u1ee3p danh s\u00e1ch \u0111\u1ea7y \u0111\u1ee7 c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS th\u01b0\u1eddng xu\u1ea5t hi\u1ec7n trong c\u00e1c cu\u1ed9c ph\u1ecfng v\u1ea5n, c\u00f9ng m\u1ed9t s\u1ed1 d\u1ea1ng \u0111\u1ec1 b\u00e0i th\u1ef1c h\u00e0nh l\u1eadp tr\u00ecnh v\u1edbi Node.js \u0111\u01a1n gi\u1ea3n. Tr\u01b0\u1edbc khi \u0111\u1ecdc \u0111\u00e1p \u00e1n, h\u00e3y th\u1eed t\u1ef1 m\u00ecnh tr\u1ea3 l\u1eddi tr\u01b0\u1edbc \u0111\u1ec3 &#8220;\u00f4n t\u1eadp&#8221; [&hellip;]<\/p>\n","protected":false},"author":213,"featured_media":83700,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109,105],"tags":[],"class_list":["post-83260","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it","category-phong-van-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS m\u1ecdi c\u1ea5p \u0111\u1ed9 - ITviec Blog<\/title>\n<meta name=\"description\" content=\"L\u01b0u ngay c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS th\u01b0\u1eddng xu\u1ea5t hi\u1ec7n trong c\u00e1c cu\u1ed9c ph\u1ecfng v\u1ea5n, bao g\u1ed3m c\u1ea3 c\u00e2u h\u1ecfi th\u1ef1c h\u00e0nh l\u1eadp tr\u00ecnh NodeJS.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS m\u1ecdi c\u1ea5p \u0111\u1ed9\" \/>\n<meta property=\"og:description\" content=\"Trong b\u00e0i vi\u1ebft n\u00e0y, ITviec \u0111\u00e3 t\u1ed5ng h\u1ee3p danh s\u00e1ch \u0111\u1ea7y \u0111\u1ee7 c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS th\u01b0\u1eddng xu\u1ea5t hi\u1ec7n trong c\u00e1c cu\u1ed9c ph\u1ecfng v\u1ea5n, c\u00f9ng m\u1ed9t s\u1ed1 d\u1ea1ng \u0111\u1ec1 b\u00e0i\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-02T07:18:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-28T16:09:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-Nodejs-vippro.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"790\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Linh Trao\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"Linh Trao\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"26 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS m\u1ecdi c\u1ea5p \u0111\u1ed9 - ITviec Blog","description":"L\u01b0u ngay c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS th\u01b0\u1eddng xu\u1ea5t hi\u1ec7n trong c\u00e1c cu\u1ed9c ph\u1ecfng v\u1ea5n, bao g\u1ed3m c\u1ea3 c\u00e2u h\u1ecfi th\u1ef1c h\u00e0nh l\u1eadp tr\u00ecnh NodeJS.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/","og_locale":"vi_VN","og_type":"article","og_title":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS m\u1ecdi c\u1ea5p \u0111\u1ed9","og_description":"Trong b\u00e0i vi\u1ebft n\u00e0y, ITviec \u0111\u00e3 t\u1ed5ng h\u1ee3p danh s\u00e1ch \u0111\u1ea7y \u0111\u1ee7 c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS th\u01b0\u1eddng xu\u1ea5t hi\u1ec7n trong c\u00e1c cu\u1ed9c ph\u1ecfng v\u1ea5n, c\u00f9ng m\u1ed9t s\u1ed1 d\u1ea1ng \u0111\u1ec1 b\u00e0i","og_url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2025-01-02T07:18:50+00:00","article_modified_time":"2025-09-28T16:09:00+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-Nodejs-vippro.jpg","type":"image\/jpeg"}],"author":"Linh Trao","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"Linh Trao","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"26 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/"},"author":{"name":"Linh Trao","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/6d1bcbfa05475cbc5a2eaf0727501a16"},"headline":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS m\u1ecdi c\u1ea5p \u0111\u1ed9","datePublished":"2025-01-02T07:18:50+00:00","dateModified":"2025-09-28T16:09:00+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/"},"wordCount":7070,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-Nodejs-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT","Ph\u1ecfng v\u1ea5n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/","url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/","name":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS m\u1ecdi c\u1ea5p \u0111\u1ed9 - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-Nodejs-vippro.jpg","datePublished":"2025-01-02T07:18:50+00:00","dateModified":"2025-09-28T16:09:00+00:00","description":"L\u01b0u ngay c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS th\u01b0\u1eddng xu\u1ea5t hi\u1ec7n trong c\u00e1c cu\u1ed9c ph\u1ecfng v\u1ea5n, bao g\u1ed3m c\u1ea3 c\u00e2u h\u1ecfi th\u1ef1c h\u00e0nh l\u1eadp tr\u00ecnh NodeJS.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-Nodejs-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-Nodejs-vippro.jpg","width":1500,"height":790,"caption":"ca\u0302u ho\u0309i pho\u0309ng va\u0302\u0301n Nodejs - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-nodejs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n NodeJS m\u1ecdi c\u1ea5p \u0111\u1ed9"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/6d1bcbfa05475cbc5a2eaf0727501a16","name":"Linh Trao","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/trao-thi-tung-linh-author-e1709892478113-100x100.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/trao-thi-tung-linh-author-e1709892478113-100x100.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/trao-thi-tung-linh-author-e1709892478113-100x100.jpg","caption":"Linh Trao"},"url":"https:\/\/itviec.com\/blog\/author\/linh-trao\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/83260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/213"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=83260"}],"version-history":[{"count":1,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/83260\/revisions"}],"predecessor-version":[{"id":91808,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/83260\/revisions\/91808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/83700"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=83260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=83260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=83260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}