{"id":14717,"date":"2024-04-19T21:47:48","date_gmt":"2024-04-19T13:47:48","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=14717"},"modified":"2024-05-10T10:06:15","modified_gmt":"2024-05-10T02:06:15","slug":"%e5%85%a5%e5%9d%91-node-js%ef%bc%88%e7%bc%96%e8%be%91%e4%b8%ad%ef%bc%89-2","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=14717","title":{"rendered":"Node.js \u5b66\u4e60\u8bb0\u5f55"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/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-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%89%8D%E8%A8%80\" >\u524d\u8a00<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E7%8E%AF%E5%A2%83\" >\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#npm_%E7%9A%84%E4%B8%80%E4%BA%9B%E5%91%BD%E4%BB%A4\" >npm \u7684\u4e00\u4e9b\u547d\u4ee4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%A8%A1%E5%9D%97%E5%AF%BC%E5%85%A5%E6%96%B9%E5%BC%8F\" >\u6a21\u5757\u5bfc\u5165\u65b9\u5f0f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%8F%98%E9%87%8F%E4%BF%AE%E9%A5%B0\" >\u53d8\u91cf\u4fee\u9970<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%90%8C%E6%AD%A5%E4%B8%8E%E5%BC%82%E6%AD%A5\" >\u540c\u6b65\u4e0e\u5f02\u6b65<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%87%AA%E5%8A%A8%E9%87%8D%E5%90%AF%E6%9C%8D%E5%8A%A1\" >\u81ea\u52a8\u91cd\u542f\u670d\u52a1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C_fs\" >\u6587\u4ef6\u64cd\u4f5c fs<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%8D%A2%E8%A1%8C%E7%AC%A6\" >\u6362\u884c\u7b26<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%BF%BD%E5%8A%A0%E6%96%87%E4%BB%B6\" >\u8ffd\u52a0\u6587\u4ef6<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%B8%93%E7%94%A8%E6%96%87%E4%BB%B6%E8%BF%BD%E5%8A%A0%E5%87%BD%E6%95%B0\" >\u4e13\u7528\u6587\u4ef6\u8ffd\u52a0\u51fd\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%86%99%E6%96%87%E4%BB%B6%E8%BF%BD%E5%8A%A0%E6%A8%A1%E5%BC%8F\" >\u5199\u6587\u4ef6\u8ffd\u52a0\u6a21\u5f0f<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%B5%81%E5%BC%8F%E5%86%99%E6%96%87%E4%BB%B6\" >\u6d41\u5f0f\u5199\u6587\u4ef6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%AF%BB%E6%96%87%E4%BB%B6\" >\u8bfb\u6587\u4ef6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%B5%81%E5%BC%8F%E8%AF%BB%E6%96%87%E4%BB%B6\" >\u6d41\u5f0f\u8bfb\u6587\u4ef6<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%8C%89%E7%BC%93%E5%AD%98%E5%A4%A7%E5%B0%8F%E8%AF%BB%E5%8F%96\" >\u6309\u7f13\u5b58\u5927\u5c0f\u8bfb\u53d6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%8C%89%E8%A1%8C%E8%AF%BB%E5%8F%96\" >\u6309\u884c\u8bfb\u53d6<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%A4%8D%E5%88%B6%E6%96%87%E4%BB%B6\" >\u590d\u5236\u6587\u4ef6<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%B8%80%E6%AC%A1%E6%80%A7%E5%A4%8D%E5%88%B6\" >\u4e00\u6b21\u6027\u590d\u5236<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%B5%81%E5%BC%8F%E5%A4%8D%E5%88%B6\" >\u6d41\u5f0f\u590d\u5236<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%85%B6%E5%AE%83%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C\" >\u5176\u5b83\u6587\u4ef6\u64cd\u4f5c<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%B7%AF%E5%BE%84_path\" >\u8def\u5f84 path<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%B7%AF%E5%BE%84%E6%8B%BC%E6%8E%A5\" >\u8def\u5f84\u62fc\u63a5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%B7%AF%E5%BE%84%E8%A7%A3%E6%9E%90\" >\u8def\u5f84\u89e3\u6790<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%85%B6%E5%AE%83%E5%87%BD%E6%95%B0\" >\u5176\u5b83\u51fd\u6570<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#Web_%E6%9C%8D%E5%8A%A1_http\" >Web \u670d\u52a1 http<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E7%AE%80%E5%8D%95%E7%9A%84_web_%E6%9C%8D%E5%8A%A1%E5%99%A8\" >\u7b80\u5355\u7684 web \u670d\u52a1\u5668<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%8E%B7%E5%8F%96%E8%AF%B7%E6%B1%82\" >\u83b7\u53d6\u8bf7\u6c42<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-29\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%BA%94%E7%94%A8\" >\u5e94\u7528<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-30\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%AF%B7%E6%B1%82\" >\u8bf7\u6c42<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-31\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%93%8D%E5%BA%94\" >\u54cd\u5e94<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-32\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#html%E3%80%81css%E3%80%81js_%E6%8B%86%E5%88%86\" >html\u3001css\u3001js \u62c6\u5206<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-33\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E9%83%A8%E7%BD%B2%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E7%AB%99\" >\u90e8\u7f72\u9759\u6001\u8d44\u6e90\u7ad9<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-34\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%A8%A1%E5%9D%97\" >\u6a21\u5757<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-35\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%AF%BC%E5%87%BA\" >\u5bfc\u51fa<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-36\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%88%AB%E5%90%8D\" >\u522b\u540d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-37\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E9%BB%98%E8%AE%A4%E5%AF%BC%E5%87%BA\" >\u9ed8\u8ba4\u5bfc\u51fa<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-38\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%8C%85%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7\" >\u5305\u7ba1\u7406\u5de5\u5177<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-39\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%8C%85%E5%AE%89%E8%A3%85%E6%88%96%E4%BE%9D%E8%B5%96%E5%AE%89%E8%A3%85\" >\u5305\u5b89\u88c5\u6216\u4f9d\u8d56\u5b89\u88c5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-40\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E9%A1%B9%E7%9B%AE\" >\u521b\u5efa\u4e00\u4e2a\u9879\u76ee<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-41\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E9%85%8D%E7%BD%AE%E5%91%BD%E4%BB%A4%E5%88%AB%E5%90%8D\" >\u914d\u7f6e\u547d\u4ee4\u522b\u540d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-42\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%8F%91%E5%B8%83%E5%8C%85\" >\u53d1\u5e03\u5305<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-43\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86\" >\u7248\u672c\u7ba1\u7406<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-44\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#express_%E6%A1%86%E6%9E%B6\" >express \u6846\u67b6<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-45\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#hello_world\" >hello world<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-46\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%B7%AF%E7%94%B1\" >\u8def\u7531<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-47\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%B7%AF%E7%94%B1%E6%96%B9%E6%B3%95\" >\u8def\u7531\u65b9\u6cd5<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-48\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%89%80%E6%9C%89%E6%96%B9%E6%B3%95\" >\u6240\u6709\u65b9\u6cd5<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-49\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%B7%AF%E7%94%B1%E8%B7%AF%E5%BE%84\" >\u8def\u7531\u8def\u5f84<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-50\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%B7%AF%E7%94%B1%E5%8F%82%E6%95%B0\" >\u8def\u7531\u53c2\u6570<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-51\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%B7%AF%E7%94%B1%E6%A8%A1%E5%9D%97%E5%8C%96\" >\u8def\u7531\u6a21\u5757\u5316<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-52\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%B8%AD%E9%97%B4%E4%BB%B6\" >\u4e2d\u95f4\u4ef6<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-53\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%85%A8%E5%B1%80%E4%B8%AD%E9%97%B4%E4%BB%B6\" >\u5168\u5c40\u4e2d\u95f4\u4ef6<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-54\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E4%B8%AD%E9%97%B4%E4%BB%B6\" >\u9759\u6001\u8d44\u6e90\u4e2d\u95f4\u4ef6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-55\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E9%98%B2%E7%9B%97%E9%93%BE\" >\u9632\u76d7\u94fe<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-56\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%B7%AF%E7%94%B1%E4%B8%AD%E9%97%B4%E4%BB%B6\" >\u8def\u7531\u4e2d\u95f4\u4ef6<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-57\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%8E%B7%E5%8F%96%E8%AF%B7%E6%B1%82%E4%BD%93%E5%86%85%E5%AE%B9\" >\u83b7\u53d6\u8bf7\u6c42\u4f53\u5185\u5bb9<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-58\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E_ejs\" >\u6a21\u677f\u5f15\u64ce ejs<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-59\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%BD%93%E9%AA%8C\" >\u4f53\u9a8c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-60\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#html_%E6%96%87%E6%9C%AC%E6%8F%92%E5%80%BC\" >html \u6587\u672c\u63d2\u503c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-61\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%88%97%E8%A1%A8%E6%B8%B2%E6%9F%93\" >\u5217\u8868\u6e32\u67d3<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-62\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%9D%A1%E4%BB%B6%E6%B8%B2%E6%9F%93\" >\u6761\u4ef6\u6e32\u67d3<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-63\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%9C%A8_express_%E4%B8%AD%E4%BD%BF%E7%94%A8_ejs\" >\u5728 express \u4e2d\u4f7f\u7528 ejs<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-64\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0\" >\u6587\u4ef6\u4e0a\u4f20<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-65\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#MySQL_%E6%95%B0%E6%8D%AE%E5%BA%93\" >MySQL \u6570\u636e\u5e93<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-66\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E9%A9%B1%E5%8A%A81%EF%BC%9Amysql\" >\u9a71\u52a81\uff1amysql<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-67\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%87%86%E5%A4%87\" >\u51c6\u5907<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-68\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93\" >\u8fde\u63a5\u6570\u636e\u5e93<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-69\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%9F%A5%E8%AF%A2\" >\u67e5\u8be2<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-70\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%96%B9%E5%BC%8F%E4%B8%80\" >\u65b9\u5f0f\u4e00<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-71\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%96%B9%E5%BC%8F%E4%BA%8C\" >\u65b9\u5f0f\u4e8c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-72\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%96%B9%E5%BC%8F%E4%B8%89\" >\u65b9\u5f0f\u4e09<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-73\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%A2%9E%E5%88%A0%E6%94%B9\" >\u589e\u5220\u6539<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-74\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%A2%9E\" >\u589e<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-75\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%94%B9\" >\u6539<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-76\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%88%A0\" >\u5220<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-77\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E8%BD%AC%E4%B9%89%E6%9F%A5%E8%AF%A2\" >\u8f6c\u4e49\u67e5\u8be2<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-78\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E9%A9%B1%E5%8A%A82%EF%BC%9Amysql2\" >\u9a71\u52a82\uff1amysql2<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-79\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%87%86%E5%A4%87-2\" >\u51c6\u5907<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-80\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%9F%A5%E8%AF%A2-2\" >\u67e5\u8be2<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-81\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%A2%9E%E5%88%A0%E6%94%B9-2\" >\u589e\u5220\u6539<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-82\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E6%8F%92%E5%85%A5\" >\u63d2\u5165<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-83\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%BF%AE%E6%94%B9\" >\u4fee\u6539<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-84\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%88%A0%E9%99%A4\" >\u5220\u9664<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-85\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#sequelize_%E6%A8%A1%E5%9E%8B\" >sequelize \u6a21\u578b<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-86\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#JSON\" >JSON<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-87\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%BD%BF%E7%94%A8_import_%E8%AF%BB%E5%8F%96\" >\u4f7f\u7528 import \u8bfb\u53d6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-88\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96\" >\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-89\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#json-server\" >json-server<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-90\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E7%94%A8%E4%BA%8E%E6%BC%94%E7%A4%BA%E7%9A%84_json\" >\u7528\u4e8e\u6f14\u793a\u7684 json<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-91\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#GET\" >GET<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-92\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E9%A1%B6%E5%B1%82%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7\" >\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-93\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E9%A1%B6%E5%B1%82%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E7%9A%84%E5%80%BC%E4%B8%BA%E6%95%B0%E7%BB%84%E6%97%B6%E8%AE%BF%E9%97%AE%E5%AD%90%E5%85%83%E7%B4%A0\" >\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\u7684\u503c\u4e3a\u6570\u7ec4\u65f6\u8bbf\u95ee\u5b50\u5143\u7d20<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-94\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#POST\" >POST<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-95\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#PUT\" >PUT<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-96\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%BF%AE%E6%94%B9%E6%95%B0%E7%BB%84%E5%AD%90%E5%85%83%E7%B4%A0\" >\u4fee\u6539\u6570\u7ec4\u5b50\u5143\u7d20<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-97\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%BF%AE%E6%94%B9%E9%A1%B6%E5%B1%82%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E5%80%BC\" >\u4fee\u6539\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\u503c<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-98\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#PATCH\" >PATCH<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-99\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%BF%AE%E6%94%B9%E6%95%B0%E7%BB%84%E5%AD%90%E5%85%83%E7%B4%A0-2\" >\u4fee\u6539\u6570\u7ec4\u5b50\u5143\u7d20<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-100\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%BF%AE%E6%94%B9%E9%A1%B6%E5%B1%82%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E5%80%BC%EF%BC%88%E6%9C%89_bug%EF%BC%9F%EF%BC%89\" >\u4fee\u6539\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\u503c\uff08\u6709 bug\uff1f\uff09<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-101\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#DELETE\" >DELETE<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-102\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E5%8F%82%E6%95%B0%E6%94%AF%E6%8C%81\" >\u53c2\u6570\u652f\u6301<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-103\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#Promise\" >Promise<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-104\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E7%A4%BA%E4%BE%8B_1\" >\u793a\u4f8b 1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-105\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E7%A4%BA%E4%BE%8B_2\" >\u793a\u4f8b 2<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-106\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E4%BC%9A%E8%AF%9D%E6%8E%A7%E5%88%B6\" >\u4f1a\u8bdd\u63a7\u5236<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-107\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#cookie\" >cookie<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-108\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#session_id\" >session id<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-109\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#token\" >token<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-110\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8\" >\u7b80\u5355\u4f7f\u7528<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-111\" href=\"https:\/\/blog.iyatt.com\/?p=14717\/#%E9%A1%B9%E7%9B%AE%E5%AE%9E%E8%B7%B5\" >\u9879\u76ee\u5b9e\u8df5<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E5%89%8D%E8%A8%80\"><\/span>\u524d\u8a00<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u524d\u9762\u521a\u521a\u5bf9 Spring Boot \u6709\u4e86\u4e2a\u6982\u5ff5\uff0c\u518d\u6765\u5b66\u5b66 Node.js\uff0c\u987a\u4fbf\u5f53\u5b66 JavaScript\uff0c\u4e3a\u540e\u9762\u5165\u524d\u7aef\u505a\u51c6\u5907\u3002<\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E7%8E%AF%E5%A2%83\"><\/span>\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Node.js 20.12.2<\/p>\n<p>\u5b98\u65b9 API \u6587\u6863\uff1a<a href=\"https:\/\/nodejs.org\/docs\/latest\/api\/\">https:\/\/nodejs.org\/docs\/latest\/api\/<\/a><br \/>\nCommonJS\uff1a<a href=\"https:\/\/nodejs.org\/api\/modules.html\">https:\/\/nodejs.org\/api\/modules.html<\/a><br \/>\nECMAScript Modules\uff1a<a href=\"https:\/\/nodejs.org\/api\/modules.html\">https:\/\/nodejs.org\/api\/modules.html<\/a><\/p>\n<h1><span class=\"ez-toc-section\" id=\"npm_%E7%9A%84%E4%B8%80%E4%BA%9B%E5%91%BD%E4%BB%A4\"><\/span>npm \u7684\u4e00\u4e9b\u547d\u4ee4<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u67e5\u770b npm \u914d\u7f6e\u6587\u4ef6\u8def\u5f84<\/p>\n<pre><code class=\"language-c\">npm config get userconfig<\/code><\/pre>\n<p>\u67e5\u770b Node.js \u4fe1\u606f<\/p>\n<pre><code class=\"language-c\">npm config ls<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714094745647.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 814px; --smush-placeholder-aspect-ratio: 814\/329;\" \/><\/p>\n<p>\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5305\uff08\u4f7f\u7528 -g \u5c31\u662f\u67e5\u5168\u5c40\u73af\u5883\uff09<\/p>\n<pre><code class=\"language-c\">npm ls [-g]<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714094979037.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 823px; --smush-placeholder-aspect-ratio: 823\/249;\" \/><\/p>\n<p>\u8bbe\u7f6e npm \u6e90\u7ad9<\/p>\n<pre><code class=\"language-c\">npm config set registry=\u6e90\u7ad9\n\n# \u9ed8\u8ba4\u6e90\u7ad9\u662f\u5b98\u65b9\u7684 http:\/\/registry.npmjs.org\n# \u4f7f\u7528\u7b2c\u4e09\u65b9\u7684\u6e90\u7ad9\uff08\u955c\u50cf\uff09\uff0c\u53ef\u4ee5\u7528\u8fd9\u4e2a\u4fee\u6539\uff0c\u6216\u6539\u56de\u5b98\u65b9\u7684<\/code><\/pre>\n<p>\u8bbe\u7f6e\u4ee3\u7406<br \/>\n\u5982\u679c\u8981\u4f7f\u7528\u5b98\u65b9\u7684\u6e90\u7ad9\uff0c\u53c8\u60f3\u8981\u4fdd\u8bc1\u4e0b\u8f7d\u901f\u5ea6\uff0c\u5c31\u4f7f\u7528\u4ee3\u7406<\/p>\n<pre><code class=\"language-c\">npm config set proxy=http:\/\/server:port\n\n# \u5982\u679c\u7528\u7684\u4ee3\u7406\u6709\u5bc6\u7801\nnpm config set proxy http:\/\/username:password@server:port\n\n# \u53d6\u6d88\u4ee3\u7406\nnpm config delete proxy<\/code><\/pre>\n<h1><span class=\"ez-toc-section\" id=\"%E6%A8%A1%E5%9D%97%E5%AF%BC%E5%85%A5%E6%96%B9%E5%BC%8F\"><\/span>\u6a21\u5757\u5bfc\u5165\u65b9\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u5206\u4e3a CommonJS\uff08CJS\uff09\u548c ECMAScript Modules\uff08ESM\uff09\u3002<\/p>\n<p>CJS \u4f7f\u7528 require \u5bfc\u5165\uff0c\u4f7f\u7528 modules.export \u6216 exports \u5bfc\u51fa\u3002ESM \u4f7f\u7528 import \u5bfc\u5165\uff0c\u4f7f\u7528 export \u5bfc\u51fa\u3002<\/p>\n<p>CJS \u5728\u8fd0\u884c\u65f6\u52a0\u8f7d\u6a21\u5757\uff0c\u5bfc\u5165\u548c\u5bfc\u51fa\u662f\u540c\u6b65\u7684\u3002ESM \u662f\u9759\u6001\u52a0\u8f7d\uff0c\u5728\u4ee3\u7801\u89e3\u6790\u7684\u65f6\u5019\u8fdb\u884c\uff0c\u5bfc\u5165\u548c\u5bfc\u51fa\u64cd\u4f5c\u662f\u5f02\u6b65\u7684\u3002<\/p>\n<p>\u6269\u5c55\u540d\u4f7f\u7528 .js \u65f6\u9ed8\u8ba4\u8bc6\u522b\u4e3a CJS\uff0c\u6269\u5c55\u540d\u4f7f\u7528 .mjs \u65f6\u9ed8\u8ba4\u8bc6\u522b\u4e3a ESM\u3002Node.js \u7684\u65e9\u671f\u7248\u672c\u53ea\u652f\u6301 CJS\uff0c\u540e\u9762\u7684\u5f00\u59cb\u652f\u6301 ESM\uff0c\u65b0\u9879\u76ee\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 ES\u3002<br \/>\n\u6216\u8005\u5728 package.json \u4e2d\u6dfb\u52a0 <strong>&quot;type&quot;: &quot;module&quot;<\/strong>\uff0c\u663e\u5f0f\u8981\u6c42\u4f7f\u7528 ESM<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714139961011.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 535px; --smush-placeholder-aspect-ratio: 535\/280;\" \/><\/p>\n<p>\u672c\u7bc7\u5b9e\u8df5\u4ee5 ESM \u8fdb\u884c\uff0c\u4e0b\u9762\u5c55\u793a\u4e24\u79cd\u65b9\u5f0f\u7684\u5bf9\u6bd4\u3002<br \/>\n\u4e0b\u9762\u7684\u4f8b\u5b50\u4f1a\u5728 8080 \u7aef\u53e3\u521b\u5efa\u4e00\u4e2a http \u670d\u52a1\uff0c\u5c55\u793a\u5b57\u7b26\u4e32\u201cHello World!\u201d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\uff1a<a href=\"http:\/\/localhost:8080\">http:\/\/localhost:8080<\/a><\/p>\n<p>CJS<\/p>\n<pre><code class=\"language-js\">const http = require(&#039;node:http&#039;);\n\nconst hostname = &#039;127.0.0.1&#039;;\nconst port = 8080;\n\nconst server = http.createServer((req, res) =&gt; {\n  res.statusCode = 200;\n  res.setHeader(&#039;Content-Type&#039;, &#039;text\/plain&#039;);\n  res.end(&#039;Hello World!\\n&#039;);\n});\n\nserver.listen(port, hostname, () =&gt; {\n  console.log(`Server running at http:\/\/${hostname}:${port}\/`);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713532973336.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 400px; --smush-placeholder-aspect-ratio: 400\/130;\" \/><\/p>\n<p>ESM<br \/>\n\u5bfc\u5165\u6307\u5b9a\u6a21\u5757<\/p>\n<pre><code class=\"language-js\">import { createServer } from &#039;node:http&#039;;\n\nconst hostname = &#039;127.0.0.1&#039;;\nconst port = 8080;\n\nconst server = createServer((req, res) =&gt; {\n  res.statusCode = 200;\n  res.setHeader(&#039;Content-Type&#039;, &#039;text\/plain&#039;);\n  res.end(&#039;Hello World!\\n&#039;);\n});\n\nserver.listen(port, hostname, () =&gt; {\n  console.log(`Server running at http:\/\/${hostname}:${port}\/`);\n});<\/code><\/pre>\n<h1><span class=\"ez-toc-section\" id=\"%E5%8F%98%E9%87%8F%E4%BF%AE%E9%A5%B0\"><\/span>\u53d8\u91cf\u4fee\u9970<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u53d8\u91cf\u4fee\u9970\u6709\u4e09\u79cd\uff1avar\u3001let \u548c const<br \/>\nconst \u548c\u4e00\u822c\u7f16\u7a0b\u8bed\u8a00\u91cc\u4e00\u6837\uff0c\u8868\u793a\u5e38\u91cf\uff0c\u58f0\u660e\u65f6\u5fc5\u987b\u521d\u59cb\u5316\uff0c\u4e14\u4e0d\u53ef\u518d\u6b21\u8d4b\u503c\uff0c\u5177\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u3002\u5982\u679c const \u4fee\u9970\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\u6216\u6570\u7ec4\uff0c\u867d\u7136\u4e0d\u80fd\u66f4\u6362\u53d8\u91cf\u7d22\u5f15\u7684\u5bf9\u8c61\u6216\u6570\u7ec4\uff0c\u4f46\u662f\u53ef\u4ee5\u4fee\u6539\u5bf9\u8c61\u5c5e\u6027\u6216\u6570\u7ec4\u5143\u7d20\u3002<br \/>\nlet \u76f8\u5f53\u4e8e\u4e00\u822c\u7f16\u7a0b\u8bed\u8a00\u91cc\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u58f0\u660e\u65f6\u53ef\u4ee5\u521d\u59cb\u5316\u4e5f\u53ef\u4ee5\u4e0d\u521d\u59cb\u5316\uff0c\u540e\u671f\u53ef\u4ee5\u518d\u6b21\u8d4b\u503c\uff0c\u4e5f\u662f\u5757\u7ea7\u4f5c\u7528\u57df\uff0c\u6bd4\u5982\u5728\u5927\u62ec\u53f7\u5185\u58f0\u660e\u7684\u53ea\u80fd\u5728\u5927\u62ec\u53f7\u5185\u8bbf\u95ee\u3002<br \/>\nvar \u4e0e\u53e6\u5916\u4e24\u79cd\u4e0d\u540c\uff0c\u5982\u679c\u5728\u51fd\u6570\u5916\u58f0\u660e\uff0c\u5219\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u90fd\u53ef\u4ee5\u8bbf\u95ee\u3002\u5728\u51fd\u6570\u5185\u58f0\u660e\uff0c\u5219\u4ec5\u5728\u51fd\u6570\u5185\u53ef\u8bbf\u95ee\u3002\u8fd8\u53ef\u4ee5\u591a\u6b21\u58f0\u660e\uff0c\u540e\u7eed\u58f0\u660e\u8986\u76d6\u524d\u9762\u7684\u58f0\u660e\u3002<\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E5%90%8C%E6%AD%A5%E4%B8%8E%E5%BC%82%E6%AD%A5\"><\/span>\u540c\u6b65\u4e0e\u5f02\u6b65<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Node.js \u63d0\u4f9b\u7684\u5f88\u591a\u51fd\u6570\u90fd\u5206\u4e3a\u540c\u6b65\u548c\u5f02\u6b65\u4e24\u4e2a\u7248\u672c\uff0c\u540c\u6b65\u7248\u51fd\u6570\u901a\u5e38\u540d\u5b57\u591a\u4e00\u4e2a Sync\u3002<br \/>\n\u540c\u6b65\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3\uff1a\u4f60\u8981\u6ce1\u8336\uff0c\u5f97\u5148\u70e7\u6c34\uff0c\u5728\u70e7\u6c34\u5f97\u8fc7\u7a0b\u4e2d\u5c31\u5728\u65c1\u8fb9\u7b49\u7740\uff0c\u76f4\u5230\u6c34\u70e7\u5f00\u4e86\uff0c\u624d\u5012\u6c34\u6ce1\u8336\u3002<br \/>\n\u5f02\u6b65\uff1a\u540c\u6837\u6ce1\u8336\uff0c\u5f00\u59cb\u70e7\u6c34\uff0c\u4f46\u662f\u4f60\u4e0d\u5728\u65c1\u8fb9\u7b49\u7740\uff0c\u8dd1\u53bb\u770b\u7535\u89c6\u4e86\uff0c\u7b49\u6c34\u70e7\u597d\u4e86\uff0c\u518d\u56de\u6765\u5012\u6c34\u6ce1\u8336\u3002<br \/>\n\u5f02\u6b65\u6267\u884c\u7684\u65f6\u5019\uff0c\u5982\u679c\u4e00\u4e2a\u64cd\u4f5c\u4f1a\u82b1\u4e00\u4e9b\u5b9e\u8df5\uff0c\u90a3\u4e48\u5c31\u4e0d\u4f1a\u5e72\u7b49\u7740\uff0c\u4f1a\u53bb\u5148\u6267\u884c\u522b\u7684\u4efb\u52a1\u3002\u5982\u679c\u662f\u540c\u6b65\u5c31\u4f1a\u7b49\u7740\u5b8c\u6210\u4e00\u4ef6\u518d\u505a\u53e6\u5916\u4e00\u4ef6\u3002\u4ece\u6027\u80fd\u6765\u8bf4\uff0c\u5f02\u6b65\u7684\u6027\u80fd\u66f4\u9ad8\uff0c\u4e0d\u4f1a\u8ba9\u8ba1\u7b97\u673a\u95f2\u7740\uff0c\u4f46\u662f\u73b0\u5b9e\u4e0d\u662f\u603b\u80fd\u5f02\u6b65\u7684\uff0c\u5982\u679c\u540e\u7eed\u7684\u64cd\u4f5c\u90fd\u4f9d\u8d56\u524d\u9762\u7684\u5de5\u4f5c\u7ed3\u679c\uff0c\u5c31\u5fc5\u987b\u91c7\u7528\u540c\u6b65\uff0c\u7b49\u5f85\u5b8c\u6210\u540e\u5f97\u5230\u7ed3\u679c\u624d\u80fd\u6267\u884c\u522b\u7684\u4efb\u52a1\u3002\u5e94\u7528\u4e2d\u6839\u636e\u5b9e\u9645\u9700\u8981\u6765\u51b3\u5b9a\u4f7f\u7528\u540c\u6b65\u8fd8\u662f\u5f02\u6b65\u3002<br \/>\n\u4e0b\u9762\u7528\u5199\u6587\u4ef6\u6765\u5c55\u793a\u540c\u6b65\u548c\u5f02\u6b65<\/p>\n<p>\u5f02\u6b65<br \/>\n\u4ece\u6267\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528\u5f02\u6b65\u5199\u6587\u4ef6\uff0c\u5199\u6587\u4ef6\u8fd9\u4e2a\u64cd\u4f5c\u4f1a\u82b1\u8d39\u201c\u8f83\u591a\u201d\u65f6\u95f4\uff0c\u4f46\u662f\u4e3b\u7ebf\u7a0b\u4e0d\u4f1a\u7b49\u7740\u5b83\u5b8c\u6210\uff0c\u800c\u662f\u5148\u53bb\u6267\u884c\u540e\u9762\u7684\u6253\u5370\u201chello world\u201d\uff0c\u5728\u6253\u5370\u8fd9\u4e2a\u64cd\u4f5c\u5b8c\u6210\u4ee5\u540e\uff0c\u5199\u6587\u4ef6\u7684\u52a8\u4f5c\u624d\u5b8c\u6210\u3002<\/p>\n<pre><code class=\"language-js\">import { writeFile } from &#039;node:fs&#039;;\n\nwriteFile(&#039;test.txt&#039;, &#039;hello world&#039;, (err) =&gt;\n{\n    if (err)\n    {\n        console.error(err);\n        return;\n    }\n    console.log(&#039;\u5199\u5165\u6210\u529f&#039;);\n});\n\nconsole.log(&#039;hello world&#039;);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713535188849.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 394px; --smush-placeholder-aspect-ratio: 394\/113;\" \/><\/p>\n<p>\u540c\u6b65<br \/>\n\u540c\u6b65\u5199\u6587\u4ef6\uff0c\u5728\u6267\u884c\u5199\u6587\u4ef6\u7684\u65f6\u5019\u5c31\u4f1a\u963b\u585e\u4e3b\u7ebf\u7a0b\uff0c\u76f4\u5230\u5b8c\u6210\u4ee5\u540e\u624d\u80fd\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u3002<\/p>\n<pre><code class=\"language-js\">import { writeFileSync } from &#039;node:fs&#039;;\n\ntry\n{\n    writeFileSync(&#039;test.txt&#039;, &#039;hello world&#039;);\n    console.log(&#039;\u5199\u5165\u6210\u529f&#039;);\n}\ncatch (err)\n{\n    console.error(err);\n    process.exit(1);\n}\n\nconsole.log(&#039;hello world&#039;);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713535536057.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 391px; --smush-placeholder-aspect-ratio: 391\/102;\" \/><\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E8%87%AA%E5%8A%A8%E9%87%8D%E5%90%AF%E6%9C%8D%E5%8A%A1\"><\/span>\u81ea\u52a8\u91cd\u542f\u670d\u52a1<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u4f7f\u7528\u7b2c\u4e09\u65b9\u7684 nodemon<br \/>\n\u5b89\u88c5<\/p>\n<pre><code class=\"language-c\">npm i -g nodemon<\/code><\/pre>\n<p>\u7528 nodemon \u8fd0\u884c\u4ee3\u7801\uff0c\u8fd9\u6837\u5728\u5f00\u53d1\u4e2d\u5c31\u4e0d\u7528\u624b\u52a8\u5173\u95ed\u518d\u91cd\u65b0\u8fd0\u884c\uff0c\u53ea\u8981\u4fdd\u5b58\u4ee3\u7801\uff0c\u5c31\u4f1a\u81ea\u52a8\u91cd\u542f Node.js \u670d\u52a1\uff0c\u5b9e\u65f6\u66f4\u65b0\u4fee\u6539<\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C_fs\"><\/span>\u6587\u4ef6\u64cd\u4f5c fs<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u4e0a\u9762\u540c\u6b65\u4e0e\u5f02\u6b65\u4e3e\u4f8b\u4f7f\u7528\u7684\u5199\u6587\u4ef6\u64cd\u4f5c\uff0c\u8fd9\u91cc\u5c31\u7565\u8fc7\u4e86\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E6%8D%A2%E8%A1%8C%E7%AC%A6\"><\/span>\u6362\u884c\u7b26<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u9ed8\u8ba4\u7684\u6362\u884c\u7b26\u662f\u4e0d\u4e00\u6837\u7684\u3002<br \/>\nWindows\uff1a\\r\\n\uff08\u56de\u8f66\u7b26+\u6362\u884c\u7b26\uff09<br \/>\nUnix\/Linux\/macOS\uff1a\\n\uff08\u6362\u884c\u7b26\uff09\uff0c\u5176\u4e2d\u65e9\u671f\u7684 macOS \u91c7\u7528\u7684\u6362\u884c\u7b26\u662f \\r\uff08\u56de\u8f66\u7b26\uff09<br \/>\n\u8981\u4fdd\u8bc1\u826f\u597d\u7684\u8de8\u5e73\u53f0\u6027\uff0c\u5c31\u4e0d\u8981\u6307\u5b9a\u67d0\u4e00\u79cd\uff0c\u4f46\u662f\u81ea\u5df1\u5199\u6bcf\u79cd\u60c5\u51b5\u53c8\u663e\u5f97\u591a\u4f59\uff0c\u56e0\u4e3a Node.js \u63d0\u4f9b\u4e86\u6362\u884c\u7b26\u3002\u50cf\u4e0b\u9762\u8fd9\u6837\u5bfc\u5165 EOL\u5c31\u884c\uff0c\u8fd9\u662f\u4e00\u4e2a\u6362\u884c\u7b26\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code class=\"language-js\">import { EOL from &#039;os&#039;;<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E8%BF%BD%E5%8A%A0%E6%96%87%E4%BB%B6\"><\/span>\u8ffd\u52a0\u6587\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%E4%B8%93%E7%94%A8%E6%96%87%E4%BB%B6%E8%BF%BD%E5%8A%A0%E5%87%BD%E6%95%B0\"><\/span>\u4e13\u7528\u6587\u4ef6\u8ffd\u52a0\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-js\">import { writeFileSync, appendFileSync, appendFile } from &#039;fs&#039;;\nimport { EOL } from &#039;os&#039;;\n\ntry\n{\n    writeFileSync(&#039;test.txt&#039;, &#039;hello world&#039; + EOL); \/\/ \u5199\u5165\u6587\u4ef6\n    appendFileSync(&#039;test.txt&#039;, &#039;hello Node.js&#039; + EOL); \/\/ \u540c\u6b65\u8ffd\u52a0\u6587\u4ef6\n}\ncatch (err)\n{\n    console.error(err);\n    process.exit(1);\n}\n\nappendFile(&#039;test.txt&#039;, &#039;hello hello&#039; + EOL, err =&gt; \/\/ \u5f02\u6b65\u8ffd\u52a0\u6587\u4ef6\n{\n    if (err)\n    {\n        console.error(err);\n        return;\n    }\n    console.log(&#039;\u5199\u5165\u6210\u529f&#039;);\n});<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E5%86%99%E6%96%87%E4%BB%B6%E8%BF%BD%E5%8A%A0%E6%A8%A1%E5%BC%8F\"><\/span>\u5199\u6587\u4ef6\u8ffd\u52a0\u6a21\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-js\">import { writeFileSync } from &#039;fs&#039;;\nimport { EOL } from &#039;os&#039;;\n\ntry\n{\n    writeFileSync(&#039;test.txt&#039;, &#039;hello world&#039; + EOL); \/\/ \u5199\u5165\u6587\u4ef6\n    writeFileSync(&#039;test.txt&#039;, &#039;hello Node.js&#039; + EOL, { flag: &#039;a&#039; }); \/\/ \u8ffd\u52a0\u6587\u4ef6\n    console.log(&#039;\u5199\u5165\u6210\u529f&#039;);\n}\ncatch\n{\n    console.error(err);\n    process.exit(1);\n}<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E6%B5%81%E5%BC%8F%E5%86%99%E6%96%87%E4%BB%B6\"><\/span>\u6d41\u5f0f\u5199\u6587\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u7c7b\u4f3c\u4e00\u822c\u7f16\u7a0b\u8bed\u8a00\u91cc\u7684\u6253\u5f00\u6587\u4ef6\u64cd\u4f5c\uff0c\u6253\u5f00\u540e\u4f1a\u521b\u5efa\u4e00\u4e2a\u64cd\u4f5c\u6587\u4ef6\u7684\u53e5\u67c4\uff0c\u901a\u8fc7\u53e5\u67c4\u6765\u8bfb\u5199\u6587\u4ef6\uff0c\u6700\u540e\u5173\u95ed\u53e5\u67c4\u3002<\/p>\n<pre><code class=\"language-js\">import { createWriteStream } from &#039;fs&#039;;\nimport { EOL } from &#039;os&#039;;\n\nconst ws = createWriteStream(&#039;test.txt&#039;);\n\nws.on(&#039;finish&#039;, () =&gt; \/\/ \u76d1\u542c\u5199\u5165\u5b8c\u6210\u4e8b\u4ef6\n{\n    console.log(&#039;\u5199\u5165\u6587\u4ef6\u6210\u529f&#039;);\n});\n\nws.on(&#039;error&#039;, (err) =&gt; \/\/ \u76d1\u542c\u5199\u5165\u9519\u8bef\u4e8b\u4ef6\n{\n    console.error(&#039;\u5199\u5165\u6587\u4ef6\u5931\u8d25:&#039;, err);\n    return;\n});\n\n\/\/ \u5199\u5165\u6587\u4ef6\nws.write(&#039;hello&#039; + EOL);\nws.write(&#039;world&#039; + EOL);\n\n\/\/ \u7ed3\u675f\u5199\u5165\nws.end();<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E8%AF%BB%E6%96%87%E4%BB%B6\"><\/span>\u8bfb\u6587\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-js\">import { readFileSync, readFile } from &#039;node:fs&#039;;\n\n\/\/ \u540c\u6b65\ntry\n{\n    const data = readFileSync(&#039;test.txt&#039;);\n    console.log(data.toString());\n}\ncatch(err)\n{\n    console.error(err);\n}\n\n\/\/ \u5f02\u6b65\nreadFile(&#039;test.txt&#039;, (err, data) =&gt;\n{\n    if (err)\n    {\n        console.error(err);\n        process.exit(1);\n    }\n    console.log(data.toString());\n});<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E6%B5%81%E5%BC%8F%E8%AF%BB%E6%96%87%E4%BB%B6\"><\/span>\u6d41\u5f0f\u8bfb\u6587\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%E6%8C%89%E7%BC%93%E5%AD%98%E5%A4%A7%E5%B0%8F%E8%AF%BB%E5%8F%96\"><\/span>\u6309\u7f13\u5b58\u5927\u5c0f\u8bfb\u53d6<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-js\">import { createReadStream } from &#039;node:fs&#039;;\n\nvar rs = createReadStream(&#039;test.txt&#039;);\n\nrs.on(&#039;data&#039;, (data) =&gt;\n{\n    console.log(data.toString());\n});\n\nrs.on(&#039;error&#039;, (error) =&gt;\n{\n    console.log(error);\n});\n\nrs.on(&#039;end&#039;, () =&gt;\n{\n    console.log(&#039;(\u8bfb\u53d6\u5b8c\u6210)&#039;);\n});<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E6%8C%89%E8%A1%8C%E8%AF%BB%E5%8F%96\"><\/span>\u6309\u884c\u8bfb\u53d6<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-js\">import { createReadStream } from &#039;node:fs&#039;;\nimport { createInterface } from &#039;node:readline&#039;;\n\nvar rs = createReadStream(&#039;test.txt&#039;);\nconst rl = createInterface(\n{\n    input: rs,\n    crlfDelay: Infinity\n});\n\nrl.on(&#039;line&#039;, (line) =&gt; {\n    console.log(line);\n});\n\nrl.on(&#039;error&#039;, (error) =&gt; {\n    console.log(error);\n});\n\nrl.on(&#039;close&#039;, () =&gt; {\n    console.log(&#039;(\u8bfb\u53d6\u5b8c\u6210)&#039;);\n});<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E5%A4%8D%E5%88%B6%E6%96%87%E4%BB%B6\"><\/span>\u590d\u5236\u6587\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4f7f\u7528\u4e00\u4e2a 69M \u7684\u89c6\u9891\u6587\u4ef6\u6d4b\u8bd5<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E4%B8%80%E6%AC%A1%E6%80%A7%E5%A4%8D%E5%88%B6\"><\/span>\u4e00\u6b21\u6027\u590d\u5236<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-js\">import { readFileSync, writeFileSync } from &#039;node:fs&#039;;\n\ntry\n{\n    const data = readFileSync(&#039;test1.mp4&#039;);\n    writeFileSync(&#039;test2.mp4&#039;, data);\n}\ncatch(error)\n{\n    console.error(error);\n}\n\nconsole.log(process.memoryUsage().rss \/ 1024 \/ 1024);<\/code><\/pre>\n<p>\u4f7f\u7528\u5185\u5b58 106M<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713600543507.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 391px; --smush-placeholder-aspect-ratio: 391\/80;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%B5%81%E5%BC%8F%E5%A4%8D%E5%88%B6\"><\/span>\u6d41\u5f0f\u590d\u5236<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-js\">import { createReadStream, createWriteStream } from &#039;node:fs&#039;;\n\nconst rs = createReadStream(&#039;test1.mp4&#039;);\nconst ws = createWriteStream(&#039;test2.mp4&#039;);\n\nrs.on(&#039;data&#039;, (chunk) =&gt; {\n   ws.write(chunk); \n});\n\/\/ \u4e5f\u53ef\u4ee5\u4f7f\u7528\u7ba1\u9053\n\/\/ rs.pipe(ws);\n\nrs.on(&#039;error&#039;, (err) =&gt; {\n    console.errot(err);\n});\n\nconsole.log(process.memoryUsage().rss \/ 1024 \/ 1024);<\/code><\/pre>\n<p>\u4f7f\u7528\u5185\u5b58 36M<br \/>\n\u5728\u8bfb\u5199\u7684\u6587\u4ef6\u8f83\u5927\u65f6\uff0c\u4f7f\u7528\u6d41\u5f0f\u8bfb\u5199\u4f1a\u6bd4\u8f83\u8282\u7701\u5185\u5b58\uff0c\u9ed8\u8ba4\u7f13\u51b2\u533a\u5927\u5c0f\u4e3a 64KB\uff0c\u4e00\u6b21\u6027\u6700\u591a\u8bfb\u5165 64KB \u5230\u5185\u5b58\uff0c\u7b49\u53d6\u51fa\u540e\u624d\u80fd\u7ee7\u7eed\u8bfb\u53d6\u3002<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713600522019.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 414px; --smush-placeholder-aspect-ratio: 414\/110;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%85%B6%E5%AE%83%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C\"><\/span>\u5176\u5b83\u6587\u4ef6\u64cd\u4f5c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5982\u91cd\u547d\u540d\u6587\u4ef6\/\u79fb\u52a8\u6587\u4ef6\uff0c\u521b\u5efa\u6587\u4ef6\u5939\uff0c\u5220\u9664\u6587\u4ef6\u5939\uff0c\u67e5\u770b\u6587\u4ef6\u4fe1\u606f\u7b49\u7b49\uff0c\u53c2\u8003\u6587\u6863\uff1a<a href=\"https:\/\/nodejs.org\/api\/fs.html\">https:\/\/nodejs.org\/api\/fs.html<\/a><\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E8%B7%AF%E5%BE%84_path\"><\/span>\u8def\u5f84 path<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u5728 CJS \u4e2d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <strong>__dirname<\/strong> \u548c <strong>__filename<\/strong> \u83b7\u53d6\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u548c\u6587\u4ef6\u81ea\u8eab\u8def\u5f84\u7684\uff0c\u4f46\u662f ESM \u4e2d\u4e0d\u53ef\u7528\u3002<\/p>\n<p>CJS<\/p>\n<pre><code class=\"language-js\">console.log(__dirname);\nconsole.log(__filename);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713602357508.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 443px; --smush-placeholder-aspect-ratio: 443\/124;\" \/><\/p>\n<p>ESM<br \/>\n\u83b7\u53d6\u76ee\u5f55\u548c\u8def\u5f84\u7684\u5b9e\u73b0\u53c2\u8003<\/p>\n<pre><code class=\"language-js\">import { fileURLToPath } from &#039;url&#039;;\nimport { dirname, resolve } from &#039;path&#039;;\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\nconst __dirname = resolve(); \/\/ \u8fd9\u79cd\u65b9\u5f0f\u83b7\u5f97\u7684\u662f\u6267\u884c\u65f6\u7684\u5de5\u4f5c\u8def\u5f84\uff0c\u5728\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u4e0b\u6267\u884c\u65f6\u7ed3\u679c\u548c\u4e0a\u9762\u4e00\u6837\n\nconsole.log(__dirname);\nconsole.log(__filename);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713602541372.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 453px; --smush-placeholder-aspect-ratio: 453\/109;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E8%B7%AF%E5%BE%84%E6%8B%BC%E6%8E%A5\"><\/span>\u8def\u5f84\u62fc\u63a5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8def\u5f84\u8fde\u63a5\u7b26\u53f7\u4e0d\u540c\uff0c\u5728 Windows \u4e0b\u662f\u53cd\u659c\u6760\uff0c\u5728 Linux \u4e0b\u662f\u659c\u6760\u3002\u901a\u8fc7 Node.js \u7684\u8def\u5f84\u62fc\u63a5\u51fd\u6570\u5c31\u80fd\u6839\u636e\u6240\u5728\u5e73\u53f0\u8fdb\u884c\u5904\u7406\uff0c\u4fdd\u8bc1\u8de8\u5e73\u53f0\u6027\u3002<br \/>\n\u83b7\u53d6\u64cd\u4f5c\u7cfb\u7edf\u8def\u5f84\u5206\u5272\u7b26<\/p>\n<pre><code class=\"language-js\">import { sep } from &#039;node:path&#039;;\n\nconsole.log(sep);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713603296240.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 401px; --smush-placeholder-aspect-ratio: 401\/95;\" \/><\/p>\n<p>\u62fc\u63a5\u8def\u5f84<\/p>\n<pre><code class=\"language-js\">import { resolve } from &#039;node:path&#039;;\n\nconst path1 = resolve(&#039;D:&#039;, &#039;hello&#039;, &#039;world&#039;, &#039;test.txt&#039;);\nconsole.log(path1);\n\nconst path2 = resolve(&#039;hello&#039;, &#039;world&#039;, &#039;test.txt&#039;);\nconsole.log(path2);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713603142741.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 549px; --smush-placeholder-aspect-ratio: 549\/105;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E8%B7%AF%E5%BE%84%E8%A7%A3%E6%9E%90\"><\/span>\u8def\u5f84\u89e3\u6790<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-js\">import { parse, resolve } from &#039;node:path&#039;;\n\nconst path = resolve(&#039;index.mjs&#039;);\nconst parseObject = parse(path);\nconsole.log(parseObject);<\/code><\/pre>\n<p>\u8fd4\u56de\u7ed3\u679c\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u6839\u76ee\u5f55\uff0c\u76ee\u5f55\uff0c\u6587\u4ef6\u540d\uff0c\u6269\u5c55\u540d\uff0c\u7eaf\u6587\u4ef6\u540d<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713603520824.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 942px; --smush-placeholder-aspect-ratio: 942\/79;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%85%B6%E5%AE%83%E5%87%BD%E6%95%B0\"><\/span>\u5176\u5b83\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u6587\u6863\uff1a<a href=\"https:\/\/nodejs.org\/api\/path.html\">https:\/\/nodejs.org\/api\/path.html<\/a><\/p>\n<h1><span class=\"ez-toc-section\" id=\"Web_%E6%9C%8D%E5%8A%A1_http\"><\/span>Web \u670d\u52a1 http<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"%E7%AE%80%E5%8D%95%E7%9A%84_web_%E6%9C%8D%E5%8A%A1%E5%99%A8\"><\/span>\u7b80\u5355\u7684 web \u670d\u52a1\u5668<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-js\">import { createServer } from &#039;node:http&#039;;\n\nconst server = createServer((req, res) =&gt;\n{\n    res.setHeader(&#039;Content-Type&#039;, &#039;text\/html;charset=UTF-8&#039;);\n    res.end(&#039;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#039;);\n})\n\nconst port = 80;\nserver.listen(port, () =&gt;\n{\n    console.log(`\u670d\u52a1\u5668\u8fd0\u884c\u5728 http:\/\/localhost:${port}\/`);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713619517747.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 362px; --smush-placeholder-aspect-ratio: 362\/155;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E8%8E%B7%E5%8F%96%E8%AF%B7%E6%B1%82\"><\/span>\u83b7\u53d6\u8bf7\u6c42<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-js\">import { createServer } from &#039;node:http&#039;;\nimport { parse } from &#039;node:url&#039;;\n\nconst server = createServer((req, res) =&gt;\n{\n    console.log(&#039;-&#039;.repeat(100));\n    console.log(&#039;\u8bf7\u6c42 URL:&#039; + req.url);\n    console.log(&#039;\u8bf7\u6c42\u65b9\u6cd5\uff1a&#039; + req.method);\n    console.log(&#039;http \u7248\u672c:&#039; + req.httpVersion);\n    console.log(&#039;\u8bf7\u6c42\u5934\uff1a&#039; + JSON.stringify(req.headers));\n    console.log(parse(req.url, true));\n    console.log(&#039;-&#039;.repeat(100));\n\n    \/\/ \u56de\u590d\u5ba2\u6237\u7aef\n    res.setHeader(&#039;Content-Type&#039;, &#039;text\/html;charset=UTF-8&#039;);\n    res.end(&#039;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#039;);\n});\n\nconst port = 80;\nserver.listen(port, () =&gt;\n{\n    console.log(`\u670d\u52a1\u5668\u8fd0\u884c\u5728 http:\/\/localhost:${port}\/`);\n});<\/code><\/pre>\n<p>\u8bbf\u95ee\uff1a<a href=\"http:\/\/localhost\/submit?s1=123&amp;s2=abc\">http:\/\/localhost\/submit?s1=123&#038;s2=abc<\/a><br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713621814698.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 391px; --smush-placeholder-aspect-ratio: 391\/128;\" \/><br \/>\n\u670d\u52a1\u5668\u7aef\u83b7\u53d6<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713621830457.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 653px; --smush-placeholder-aspect-ratio: 653\/285;\" \/><\/p>\n<p>\u53e6\u5916\u4e00\u79cd\u89e3\u6790\u65b9\u5f0f<\/p>\n<pre><code class=\"language-js\">import { createServer } from &#039;node:http&#039;;\n\nconst server = createServer((req, res) =&gt;\n{\n    console.log(&#039;-&#039;.repeat(100));\n    console.log(&#039;\u8bf7\u6c42 URL:&#039; + req.url);\n    console.log(&#039;\u8bf7\u6c42\u65b9\u6cd5\uff1a&#039; + req.method);\n    console.log(&#039;http \u7248\u672c:&#039; + req.httpVersion);\n    console.log(&#039;\u8bf7\u6c42\u5934\uff1a&#039; + JSON.stringify(req.headers));\n    let url = new URL(req.url, `http:\/\/${req.headers.host}`);\n    console.log(&#039;pathname: &#039; + url.pathname);\n    console.log(&#039;search: &#039; + url.search);\n    console.log(&#039;searchParams: &#039; + url.searchParams);\n    console.log(url.searchParams.get(&#039;s1&#039;) + &#039; &#039; + url.searchParams.get(&#039;s2&#039;));\n    console.log(&#039;-&#039;.repeat(100));\n\n    \/\/ \u56de\u590d\u5ba2\u6237\u7aef\n    res.setHeader(&#039;Content-Type&#039;, &#039;text\/html;charset=UTF-8&#039;);\n    res.end(&#039;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#039;);\n})\n\nconst port = 80;\nserver.listen(port, () =&gt;\n{\n    console.log(`\u670d\u52a1\u5668\u8fd0\u884c\u5728 http:\/\/localhost:${port}\/`);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713622163862.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 376px; --smush-placeholder-aspect-ratio: 376\/257;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%BA%94%E7%94%A8\"><\/span>\u5e94\u7528<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%E8%AF%B7%E6%B1%82\"><\/span>\u8bf7\u6c42<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-js\">import { createServer } from &#039;node:http&#039;;\n\nconst server = createServer((req, res) =&gt; \n{\n    let { method } = req;\n    let { pathname } = new URL(req.url, `http:\/\/${req.headers.host}`);\n\n    res.setHeader(&#039;Content-Type&#039;, &#039;text\/html; charset=utf-8&#039;);\n    console.log(method, pathname);\n    if (method === &#039;GET&#039; &amp;&amp; pathname === &#039;\/login&#039;)\n    {\n        res.end(&#039;\u767b\u5f55\u9875\u9762&#039;);\n    }\n    else if (method === &#039;GET&#039; &amp;&amp; pathname === &#039;\/register&#039;)\n    {\n        res.end(&#039;\u6ce8\u518c\u9875\u9762&#039;);\n    }\n    else\n    {\n        res.statusCode = 404;\n        res.end(&#039;Not Found&#039;);\n    }\n});\n\nconst port = 80;\nserver.listen(port, () =&gt;\n{\n    console.log(`\u670d\u52a1\u5668\u8fd0\u884c\u5728 http:\/\/localhost:${port}\/`);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713627333449.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 274px; --smush-placeholder-aspect-ratio: 274\/119;\" \/><br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713627483643.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 277px; --smush-placeholder-aspect-ratio: 277\/129;\" \/><br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713627496844.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 313px; --smush-placeholder-aspect-ratio: 313\/148;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E5%93%8D%E5%BA%94\"><\/span>\u54cd\u5e94<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u52a0\u8f7d html \u6587\u4ef6\u4f5c\u4e3a\u54cd\u5e94\u5185\u5bb9<\/p>\n<p>index.mjs<\/p>\n<pre><code class=\"language-js\">import { createServer } from &#039;node:http&#039;;\nimport { readFileSync } from &#039;node:fs&#039;;\n\nconst server = createServer((req, res) =&gt; \n{\n    let data = readFileSync(&#039;index.html&#039;);\n    res.setHeader(&#039;Content-Type&#039;, &#039;text\/html; charset=utf-8&#039;);\n    res.end(data);\n});\n\nconst port = 80;\nserver.listen(port, () =&gt;\n{\n    console.log(`\u670d\u52a1\u5668\u8fd0\u884c\u5728 http:\/\/localhost:${port}\/`);\n});<\/code><\/pre>\n<p>index.html<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;zh&quot;&gt;\n    &lt;head&gt;\n        &lt;meta charset=&quot;UTF-8&quot;&gt;\n        &lt;title&gt;\u8868\u683c&lt;\/title&gt;\n        &lt;style&gt;\n            td{\n                padding: 20px 40px;\n            }\n            table tr:nth-child(odd){\n                background-color: #f11212;\n            }\n            table tr:nth-child(even){\n                background-color: #5b0af1;\n            }\n            table, td{\n                border-collapse: collapse;\n            }\n        &lt;\/style&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;table border=&quot;1&quot;&gt;\n            &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;td&gt;2&lt;\/td&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;\n            &lt;tr&gt;&lt;td&gt;4&lt;\/td&gt;&lt;td&gt;5&lt;\/td&gt;&lt;td&gt;6&lt;\/td&gt;&lt;\/tr&gt;\n            &lt;tr&gt;&lt;td&gt;7&lt;\/td&gt;&lt;td&gt;8&lt;\/td&gt;&lt;td&gt;9&lt;\/td&gt;&lt;\/tr&gt;\n            &lt;tr&gt;&lt;td&gt;10&lt;\/td&gt;&lt;td&gt;11&lt;\/td&gt;&lt;td&gt;12&lt;\/td&gt;&lt;\/tr&gt;\n        &lt;\/table&gt;\n        &lt;script&gt;\n            let tds = document.querySelectorAll(&#039;td&#039;);\n            tds.forEach(item =&gt; {\n                item.onclick = function(){\n                    item.style.backgroundColor = &#039;#000000&#039;;\n                }\n            })\n        &lt;\/script&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>\u70b9\u51fb\u5355\u5143\u683c\u53d8\u8272<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713629949316.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 358px; --smush-placeholder-aspect-ratio: 358\/262;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"html%E3%80%81css%E3%80%81js_%E6%8B%86%E5%88%86\"><\/span>html\u3001css\u3001js \u62c6\u5206<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>index.html<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;zh&quot;&gt;\n    &lt;head&gt;\n        &lt;meta charset=&quot;UTF-8&quot;&gt;\n        &lt;title&gt;\u8868\u683c&lt;\/title&gt;\n        &lt;link rel=&quot;stylesheet&quot; href=&quot;index.css&quot;&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;table border=&quot;1&quot;&gt;\n            &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;td&gt;2&lt;\/td&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;\n            &lt;tr&gt;&lt;td&gt;4&lt;\/td&gt;&lt;td&gt;5&lt;\/td&gt;&lt;td&gt;6&lt;\/td&gt;&lt;\/tr&gt;\n            &lt;tr&gt;&lt;td&gt;7&lt;\/td&gt;&lt;td&gt;8&lt;\/td&gt;&lt;td&gt;9&lt;\/td&gt;&lt;\/tr&gt;\n            &lt;tr&gt;&lt;td&gt;10&lt;\/td&gt;&lt;td&gt;11&lt;\/td&gt;&lt;td&gt;12&lt;\/td&gt;&lt;\/tr&gt;\n        &lt;\/table&gt;\n        &lt;script src=&quot;index.js&quot;&gt;&lt;\/script&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>index.css<\/p>\n<pre><code class=\"language-css\">td{\n    padding: 20px 40px;\n}\ntable tr:nth-child(odd){\n    background-color: #f11212;\n}\ntable tr:nth-child(even){\n    background-color: #5b0af1;\n}\ntable, td{\n    border-collapse: collapse;\n}<\/code><\/pre>\n<p>index.js<\/p>\n<pre><code class=\"language-js\">let tds = document.querySelectorAll(&#039;td&#039;);\ntds.forEach(item =&gt; {\n    item.onclick = function(){\n        item.style.backgroundColor = &#039;#000000&#039;;\n    }\n})<\/code><\/pre>\n<p>main.mjs<\/p>\n<pre><code class=\"language-js\">import { createServer } from &#039;node:http&#039;;\nimport { readFileSync } from &#039;node:fs&#039;;\n\nconst server = createServer((req, res) =&gt; \n{\n    var { pathname } = new URL(req.url, `http:\/\/${req.headers.host}`);\n    if (pathname === &#039;\/&#039;){\n        res.setHeader(&#039;Content-Type&#039;, &#039;text\/html; charset=utf-8&#039;);\n        let html = readFileSync(&#039;index.html&#039;, &#039;utf-8&#039;);\n        res.end(html);\n    }\n    else if (pathname.endsWith(&#039;.css&#039;)){\n        res.setHeader(&#039;Content-Type&#039;, &#039;text\/css; charset=utf-8&#039;);\n        let css = readFileSync(pathname.slice(1), &#039;utf-8&#039;);\n        res.end(css);\n    }\n    else if (pathname.endsWith(&#039;.js&#039;)){\n        res.setHeader(&#039;Content-Type&#039;, &#039;application\/javascript; charset=utf-8&#039;);\n        let js = readFileSync(pathname.slice(1), &#039;utf-8&#039;);\n        res.end(js);\n    }\n    else{\n        res.statusCode = 404;\n        res.end(&#039;404 Not Found&#039;);\n    }\n});\n\nconst port = 80;\nserver.listen(port, () =&gt;\n{\n    console.log(`\u670d\u52a1\u5668\u8fd0\u884c\u5728 http:\/\/localhost:${port}\/`);\n});<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"%E9%83%A8%E7%BD%B2%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E7%AB%99\"><\/span>\u90e8\u7f72\u9759\u6001\u8d44\u6e90\u7ad9<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u7528\u7684\u6211\u4e3b\u9875\u7684\u6e90\u7801\uff0c\u4e3b\u9875\u5730\u5740\uff1a<a href=\"https:\/\/iyatt.com\">https:\/\/iyatt.com<\/a><br \/>\n\u6587\u4ef6\u7ed3\u6784\u5982\u56fe<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713691017053.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 264px; --smush-placeholder-aspect-ratio: 264\/399;\" \/><\/p>\n<p>\u4e0b\u9762\u662f Node.js \u4ee3\u7801<\/p>\n<pre><code class=\"language-js\">import { createServer } from &#039;node:http&#039;;\nimport { readFile } from &#039;node:fs&#039;;\nimport { extname, resolve } from &#039;node:path&#039;;\n\nconst root = resolve(&#039;homepage&#039;); \/\/ \u7f51\u7ad9\u6839\u76ee\u5f55\nconst mimeTypes = { \/\/ \u652f\u6301\u7684\u6587\u4ef6\u7c7b\u578b\u548c\u5bf9\u5e94\u7684MIME\u7c7b\u578b\uff08\u5f00\u53d1\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u6a21\u5757\uff09\n    &#039;.html&#039;: &#039;text\/html; charset=utf-8&#039;,\n    &#039;.css&#039;: &#039;text\/css&#039;,\n    &#039;.js&#039;: &#039;application\/javascript&#039;,\n    &#039;.png&#039;: &#039;image\/png&#039;,\n    &#039;.jpg&#039;: &#039;image\/jpeg&#039;,\n    &#039;.gif&#039;: &#039;image\/gif&#039;,\n    &#039;.ico&#039;: &#039;image\/x-icon&#039;,\n};\n\nconst server = createServer((req, res) =&gt; \n{\n    const { pathname } = new URL(req.url, `http:\/\/${req.headers.host}`);\n\n    if (req.method !== &#039;GET&#039;) { \/\/ \u53ea\u5904\u7406 GET \u8bf7\u6c42\n        res.statusCode = 405;\n        res.end(&#039;&lt;h1&gt;405 Method Not Allowed&lt;\/h1&gt;&#039;);\n        return;\n    }\n\n    if (pathname === &#039;\/&#039;) { \/\/ \u8bbf\u95ee\u6839\u76ee\u5f55\u8df3\u8f6c index.html\n        res.statusCode = 301;\n        res.setHeader(&#039;Location&#039;, &#039;\/index.html&#039;);\n        res.end();\n    }\n    else {\n        const ext = extname(pathname);\n        readFile(resolve(root, pathname.slice(1)), (err, data) =&gt; {\n            if (err) {\n                switch (err.code) {\n                    case &#039;ENOENT&#039;: { \/\/ \u6587\u4ef6\u4e0d\u5b58\u5728\n                        res.statusCode = 404;\n                        res.end(&#039;&lt;h1&gt;404 Not Found&lt;\/h1&gt;&#039;);\n                        break;\n                    }\n                    case &#039;EPERM&#039;: { \/\/ \u6743\u9650\u4e0d\u8db3\n                        res.statusCode = 403;\n                        res.end(&#039;&lt;h1&gt;403 Forbidden&lt;\/h1&gt;&#039;);\n                        break;\n                    }\n                    default: { \/\/ \u5176\u4ed6\u9519\u8bef\n                        res.statusCode = 500;\n                        res.end(&#039;&lt;h1&gt;500 Internal Server Error&lt;\/h1&gt;&#039;);\n                        break;\n                    }\n                }\n            }\n            else {\n                if (mimeTypes[ext]) { \/\/ \u8bbe\u5b9a\u5df2\u77e5\u7684 Content-Type\n                    res.setHeader(&#039;Content-Type&#039;, mimeTypes[ext]);\n                }\n                else { \/\/ \u672a\u77e5\u7684\u4ea7\u751f\u4e0b\u8f7d\u884c\u4e3a\n                    res.setHeader(&#039;Content-Type&#039;, &#039;application\/octet-stream&#039;);\n                }\n                res.end(data);\n            }\n        });\n    }\n});\n\nconst port = 80;\nserver.listen(port, () =&gt;\n{\n    console.log(`\u670d\u52a1\u5668\u8fd0\u884c\u5728 http:\/\/localhost:${port}\/`);\n});<\/code><\/pre>\n<p>\u6b63\u5e38\u8bbf\u95ee<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713687579805.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 926px; --smush-placeholder-aspect-ratio: 926\/838;\" \/><\/p>\n<p>\u8bbf\u95ee\u8d44\u6e90\u4e2d\u7684\u4e00\u5f20\u56fe\u7247<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713687935713.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 883px; --smush-placeholder-aspect-ratio: 883\/805;\" \/><\/p>\n<p>\u627e\u4e0d\u5230\u6587\u4ef6<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713687596750.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 310px; --smush-placeholder-aspect-ratio: 310\/156;\" \/><\/p>\n<p>\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u6587\u4ef6<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713687744731.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 343px; --smush-placeholder-aspect-ratio: 343\/149;\" \/><\/p>\n<p>\u4e0b\u8f7d\u884c\u4e3a<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713687858524.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 997px; --smush-placeholder-aspect-ratio: 997\/282;\" \/><\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E6%A8%A1%E5%9D%97\"><\/span>\u6a21\u5757<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u57fa\u4e8e ESM \u7684\u6a21\u5757\u5bfc\u51fa<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%AF%BC%E5%87%BA\"><\/span>\u5bfc\u51fa<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><strong>\u81ea\u5b9a\u4e49\u6a21\u5757\u5b9e\u73b0 1<\/strong><br \/>\n\u9488\u5bf9\u5355\u4e2a\u51fd\u6570\u3001\u53d8\u91cf\u5bfc\u51fa\uff0c\u5728\u8981\u5bfc\u51fa\u7684\u51fd\u6570\u548c\u53d8\u91cf\u524d\u52a0\u4e0a export<\/p>\n<p>modules.mjs<\/p>\n<pre><code class=\"language-js\">export function testFunction1() {\n    console.log(&#039;\u6d4b\u8bd5\u51fd\u65701&#039;);\n}\n\nexport function testFunction2() {\n    console.log(&#039;\u6d4b\u8bd5\u51fd\u65702&#039;);\n}\n\nexport const testConstant = &#039;\u8fd9\u662f\u4e00\u4e2a\u5e38\u91cf&#039;;<\/code><\/pre>\n<hr \/>\n<p><strong>\u81ea\u5b9a\u4e49\u6a21\u5757\u5b9e\u73b0 2<\/strong><br \/>\n\u96c6\u4e2d\u5bfc\u51fa\uff0c\u4f7f\u7528 export {} \u628a\u8981\u5bfc\u51fa\u7684\u51fd\u6570\u3001\u53d8\u91cf\u653e\u8fdb\u53bb<\/p>\n<p>modules.mjs<\/p>\n<pre><code class=\"language-mjs\">function testFunction1() {\n    console.log(&#039;\u6d4b\u8bd5\u51fd\u65701&#039;);\n}\n\nfunction testFunction2() {\n    console.log(&#039;\u6d4b\u8bd5\u51fd\u65702&#039;);\n}\n\nconst testConstant = &#039;\u8fd9\u662f\u4e00\u4e2a\u5e38\u91cf&#039;;\n\nexport { testFunction1, testFunction2, testConstant }<\/code><\/pre>\n<hr \/>\n<p><strong>\u4f7f\u7528\u6a21\u5757<\/strong><br \/>\nindex.mjs<\/p>\n<pre><code class=\"language-js\">export function testFunction1() {\n    console.log(&#039;\u6d4b\u8bd5\u51fd\u65701&#039;);\n}\n\nexport function testFunction2() {\n    console.log(&#039;\u6d4b\u8bd5\u51fd\u65702&#039;);\n}\n\nexport const testConstant = &#039;\u8fd9\u662f\u4e00\u4e2a\u5e38\u91cf&#039;;<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713695822820.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 377px; --smush-placeholder-aspect-ratio: 377\/123;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%88%AB%E5%90%8D\"><\/span>\u522b\u540d<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u7ed9\u8981\u5bfc\u51fa\u7684\u5185\u5bb9\u8bbe\u7f6e\u522b\u540d\uff0c\u4f7f\u7528\u96c6\u4e2d\u5bfc\u51fa<\/p>\n<p>modules.mjs<\/p>\n<pre><code class=\"language-mjs\">function testFunction1() {\n    console.log(&#039;\u6d4b\u8bd5\u51fd\u65701&#039;);\n}\n\nfunction testFunction2() {\n    console.log(&#039;\u6d4b\u8bd5\u51fd\u65702&#039;);\n}\n\nconst testConstant = &#039;\u8fd9\u662f\u4e00\u4e2a\u5e38\u91cf&#039;;\n\nexport { testFunction1 as test1, testFunction2 as test2, testConstant as test }<\/code><\/pre>\n<p>index.mjs<\/p>\n<pre><code class=\"language-js\">import { test1, test2, test } from &quot;.\/modules.mjs&quot;;\n\nconsole.log(test);\ntest1();\ntest2();<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E9%BB%98%E8%AE%A4%E5%AF%BC%E5%87%BA\"><\/span>\u9ed8\u8ba4\u5bfc\u51fa<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u524d\u9762\u7684\u666e\u901a\u5bfc\u51fa\uff0c\u5728\u5bfc\u5165\u4f7f\u7528\u7684\u65f6\u5019\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u62ec\u53f7\uff0c\u800c\u9ed8\u8ba4\u5bfc\u51fa\u53ef\u4ee5\u4e0d\u7528\u6dfb\u52a0\u62ec\u53f7\u3002\u53ea\u662f\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u53ea\u5141\u8bb8\u4e00\u4e2a\u9ed8\u8ba4\u5bfc\u51fa\uff0c\u4f7f\u7528\u65b9\u6cd5\u5728\u666e\u901a\u5bfc\u51fa\u7684\u57fa\u7840\u4e0a\u628a export \u6362\u6210 export default \u5c31\u884c\u3002\u5982\u679c\u662f\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u4e3a\u9ed8\u8ba4\u5bfc\u51fa\u4e0d\u80fd\u76f4\u63a5\u5728 const\/var\/let \u524d\u5199\uff0c\u8981\u989d\u5916\u5199\u5bfc\u51fa\u3002\u6bd4\u5982<\/p>\n<pre><code class=\"language-js\">const testConstant = &#039;\u8fd9\u662f\u4e00\u4e2a\u5e38\u91cf&#039;;\nexport default testConstant;<\/code><\/pre>\n<p>\u4e0b\u9762\u5c06\u4e00\u4e2a\u51fd\u6570\u9ed8\u8ba4\u5bfc\u51fa<br \/>\nmodules.mjs<\/p>\n<pre><code class=\"language-js\">export function testFunction1() {\n    console.log(&#039;\u6d4b\u8bd5\u51fd\u65701&#039;);\n}\n\nexport default function testFunction2() {\n    console.log(&#039;\u6d4b\u8bd5\u51fd\u65702&#039;);\n}<\/code><\/pre>\n<p>\u4f7f\u7528<br \/>\n\u5982\u679c\u4e00\u6b21\u6027\u5bfc\u5165\u591a\u4e2a\uff0c\u9ed8\u8ba4\u5bfc\u51fa\u7684\u5fc5\u987b\u5199\u5728\u524d\u9762<br \/>\nindex.mjs<\/p>\n<pre><code class=\"language-js\">import testFunction2, { testFunction1 } from &quot;.\/modules.mjs&quot;;\n\ntestFunction1();\ntestFunction2();<\/code><\/pre>\n<h1><span class=\"ez-toc-section\" id=\"%E5%8C%85%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7\"><\/span>\u5305\u7ba1\u7406\u5de5\u5177<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Node.js \u7684\u5b98\u65b9\u5305\u7ba1\u7406\u5de5\u5177\u662f npm\uff0c\u4e5f\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u6bd4\u5982 yarn\u3002<br \/>\n\u5173\u4e8e npm \u7684\u5b98\u65b9\u8bf4\u660e\uff1a<a href=\"https:\/\/nodejs.org\/en\/learn\/getting-started\/an-introduction-to-the-npm-package-manager\">https:\/\/nodejs.org\/en\/learn\/getting-started\/an-introduction-to-the-npm-package-manager<\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%8C%85%E5%AE%89%E8%A3%85%E6%88%96%E4%BE%9D%E8%B5%96%E5%AE%89%E8%A3%85\"><\/span>\u5305\u5b89\u88c5\u6216\u4f9d\u8d56\u5b89\u88c5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5b89\u88c5\u5305\u4f7f\u7528\u547d\u4ee4<\/p>\n<pre><code class=\"language-bash\">npm install\n# \u6216\nnpm i<\/code><\/pre>\n<p>\u5b89\u88c5\u6307\u5b9a\u5305\u53ef\u4ee5\u5728\u547d\u4ee4\u540e\u8ddf\u4e0a\u5305\u540d\uff0c\u641c\u7d22\u5305\u53ef\u524d\u5f80\uff1a<a href=\"https:\/\/www.npmjs.com\/\">https:\/\/www.npmjs.com\/<\/a><br \/>\n\u5982\u679c\u8981\u5168\u5c40\u5b89\u88c5\u5c31\u52a0\u4e0a\u53c2\u6570 -g\uff0c\u4e00\u822c\u662f\u547d\u4ee4\u5de5\u5177\u91c7\u7528\u5168\u5c40\u5b89\u88c5\u7684\u65b9\u5f0f\uff0c\u8fd9\u6837\u4e0d\u7ba1\u5728\u4ec0\u4e48\u8def\u5f84\u4e0b\u90fd\u80fd\u4f7f\u7528\uff0c\u53ef\u4ee5\u53c2\u8003\u8981\u5b89\u88c5\u7684\u4e1c\u897f\u7684\u6587\u6863\u51b3\u5b9a\u4ec0\u4e48\u65b9\u5f0f\u5b89\u88c5\u3002\u4f7f\u7528\u547d\u4ee4\u67e5\u770b\u5168\u5c40\u5b89\u88c5\u8def\u5f84<\/p>\n<pre><code class=\"language-c\">npm root -g<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713713955393.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 443px; --smush-placeholder-aspect-ratio: 443\/110;\" \/><\/p>\n<p>\u5f53\u7136\u547d\u4ee4\u5de5\u5177\u4e5f\u53ef\u4ee5\u91c7\u7528\u975e\u5168\u5c40\u5b89\u88c5\u65b9\u5f0f\uff0c\u8fd0\u884c\u7684\u65f6\u5019\u547d\u4ee4\u524d\u9762\u52a0\u4e0a npx \u5c31\u884c\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u4f7f\u7528 -g \u53c2\u6570\uff0c\u9ed8\u8ba4\u5b89\u88c5\u662f\u5728\u5f53\u524d\u5de5\u4f5c\u8def\u5f84\u4e0b\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939 node_modules\uff0c\u5e76\u5728\u91cc\u9762\u653e\u7f6e\u5b89\u88c5\u7684\u4e1c\u897f\u3002\u53e6\u5916\u5728\u5de5\u4f5c\u8def\u5f84\u4e0b\u4f1a\u4ea7\u751f\u4e00\u4e2a package-lock.json \u6587\u4ef6\uff0c\u91cc\u9762\u4f1a\u8bb0\u5f55\u5b89\u88c5\u7684\u5305\u7684\u540d\u5b57\u3001\u7248\u672c\u3001\u5730\u5740\u3001\u6821\u9a8c\u4fe1\u606f\u3002\u5728\u53d1\u5e03\u81ea\u5df1\u5f00\u53d1\u7684\u8f6f\u4ef6\u7684\u65f6\u5019\u901a\u5e38\u4e0d\u6253\u5305 node_modules \u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u7f29\u5c0f\u6253\u5305\u4f53\u79ef\uff0c\u5728\u7528\u6237\u4f7f\u7528\u8fd9\u4e2a\u8f6f\u4ef6\u7684\u65f6\u5019\u53ef\u4ee5\u901a\u8fc7\u4e0a\u9762\u7684\u5b89\u88c5\u547d\u4ee4\u6765\u81ea\u52a8\u5b8c\u6210\u4f9d\u8d56\u5b89\u88c5\uff0c\u5b89\u88c5\u7684\u65f6\u5019\u4e0d\u9700\u8981\u6307\u5b9a\u5305\u540d\uff0c\u4f1a\u8bfb\u53d6 package-lock.json \u6587\u4ef6\u83b7\u53d6\u5f00\u53d1\u8005\u4f7f\u7528\u7684\u4f9d\u8d56\u3002<br \/>\n\u7ad9\u5728\u8f6f\u4ef6\u5f00\u53d1\u8005\u7684\u89d2\u5ea6\uff0c\u5bf9\u4e8e\u4f7f\u7528\u7684\u4f9d\u8d56\u53c8\u5206\u666e\u901a\u4f9d\u8d56\u548c\u5f00\u53d1\u4f9d\u8d56\uff0c\u9ed8\u8ba4\u5b89\u88c5\u662f\u6807\u6ce8\u4e3a\u666e\u901a\u4f9d\u8d56\uff0c\u5373\u4f7f\u7528 -S \u53c2\u6570\uff0c\u4f7f\u7528 -D \u53c2\u6570\u5b89\u88c5\u7684\u5219\u4e3a\u5f00\u53d1\u4f9d\u8d56\u3002\u5f00\u53d1\u8005\u7f16\u5199\u4e00\u4e2a\u8f6f\u4ef6\u5b89\u88c5\u7684\u666e\u901a\u4f9d\u8d56\uff0c\u53d1\u5e03\u51fa\u53bb\uff0c\u4f7f\u7528 npm i \u81ea\u52a8\u5b89\u88c5\u4f9d\u8d56\u4f1a\u540c\u6837\u5b89\u88c5\u3002\u800c\u5f00\u53d1\u4f9d\u8d56\u4e00\u822c\u53ea\u662f\u7528\u4e8e\u5f00\u53d1\u8005\u6d4b\u8bd5\u4f7f\u7528\uff0c\u7528\u6237\u8fd0\u884c\u5f00\u53d1\u8005\u7f16\u5199\u7684\u8f6f\u4ef6\u5e76\u4e0d\u4f9d\u8d56\uff0c\u53ef\u4ee5\u4e0d\u9700\u8981\u5b89\u88c5\uff0c\u5f00\u53d1\u8005\u4f7f\u7528 -D \u5b89\u88c5\u8fd9\u4e9b\u4f9d\u8d56\uff0c\u5219\u53d1\u5e03\u51fa\u53bb\uff0c\u7528\u6237\u5b89\u88c5\u4f9d\u8d56\u65f6\u5c31\u4e0d\u4f1a\u5b89\u88c5\u8fd9\u4e9b\u4f9d\u8d56\u3002\uff08\u4e0b\u56fe\u662f\u6587\u6863\u539f\u6587\uff09<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713714323190.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 762px; --smush-placeholder-aspect-ratio: 762\/499;\" \/><\/p>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5982\u679c\u5f00\u53d1\u8005\u7f16\u5199\u4e00\u4e2a\u8f6f\u4ef6\u7528\u5230\u7684\u67d0\u4e9b\u4f9d\u8d56\u7684\u529f\u80fd\u662f\u8981\u96c6\u6210\u5230\u7f16\u5199\u7684\u8f6f\u4ef6\u4e2d\uff0c\u8fd9\u79cd\u4f9d\u8d56\u5f00\u53d1\u8005\u5c31\u8981\u5b89\u88c5\u4e3a\u666e\u901a\u4f9d\u8d56\uff0c\u4e5f\u53ef\u4ee5\u53eb\u505a\u751f\u4ea7\u4f9d\u8d56\u3002\u540c\u65f6\u53e6\u5916\u5b58\u5728\u4e00\u4e9b\u4f9d\u8d56\uff0c\u5b83\u4eec\u4e0d\u662f\u8f6f\u4ef6\u529f\u80fd\u7684\u7ec4\u6210\uff0c\u4f46\u662f\u662f\u5f00\u53d1\u8005\u8fdb\u884c\u5f00\u53d1\u9700\u8981\u4f7f\u7528\u7684\u5de5\u5177\u6216\u8005\u6d4b\u8bd5\u6846\u67b6\uff0c\u53ea\u662f\u5f00\u53d1\u8005\u9700\u8981\uff0c\u8f6f\u4ef6\u8fd0\u884c\u672c\u8eab\u4e0d\u7528\uff0c\u5f00\u53d1\u8005\u5c31\u8981\u628a\u8fd9\u4e9b\u4f9d\u8d56\u4f5c\u4e3a\u5f00\u53d1\u4f9d\u8d56\u5b89\u88c5\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E9%A1%B9%E7%9B%AE\"><\/span>\u521b\u5efa\u4e00\u4e2a\u9879\u76ee<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u7ec8\u7aef\u5de5\u4f5c\u8def\u5f84\u5207\u6362\u5230\u6587\u4ef6\u5939\u4e0b\uff0c\u6267\u884c<\/p>\n<pre><code class=\"language-c\">npm init<\/code><\/pre>\n<p>\u9ed8\u8ba4\u9879\u76ee\u540d\u4f1a\u4f7f\u7528\u6587\u4ef6\u5939\u7684\u540d\u79f0\uff0c\u4f46\u662f\u9879\u76ee\u540d\u79f0\u4e0d\u80fd\u7528\u4e2d\u6587\uff0c\u5982\u679c\u6587\u4ef6\u5939\u542b\u6709\u4e2d\u6587\uff0c\u5c31\u81ea\u884c\u8bbe\u7f6e\u82f1\u6587\u540d\u79f0\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8bbe\u7f6e\u5176\u5b83\u540d\u79f0<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713751740969.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 712px; --smush-placeholder-aspect-ratio: 712\/709;\" \/><\/p>\n<p>\u4e0a\u9762\u7684\u547d\u4ee4\u5c31\u662f\u5f15\u5bfc\u521b\u5efa\u4e00\u4e2a package.json \u6587\u4ef6<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713751783566.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 789px; --smush-placeholder-aspect-ratio: 789\/346;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E9%85%8D%E7%BD%AE%E5%91%BD%E4%BB%A4%E5%88%AB%E5%90%8D\"><\/span>\u914d\u7f6e\u547d\u4ee4\u522b\u540d<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u6211\u5199\u4e86\u4e00\u4e2a\u6e90\u6587\u4ef6 index.mjs<\/p>\n<pre><code class=\"language-js\">console.log(&#039;Hello, world!&#039;);<\/code><\/pre>\n<p>\u4fee\u6539 package.json<br \/>\n\u4e2d scripts \u90e8\u5206\uff0c\u6dfb\u52a0\u4e86\u4e24\u4e2a\u522b\u540d server \u548c start \u548c\u522b\u540d\u5bf9\u5e94\u6267\u884c\u7684\u547d\u4ee4<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713751962517.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 784px; --smush-placeholder-aspect-ratio: 784\/378;\" \/><\/p>\n<p>\u5c31\u53ef\u4ee5\u4f7f\u7528 <strong>npm run \u522b\u540d<\/strong> \u7684\u65b9\u5f0f\u6267\u884c\uff0c\u5176\u4e2d start \u8fd9\u4e2a\u522b\u540d\u7279\u6b8a\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 <strong>npm start<\/strong> \u6267\u884c<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713752004920.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 480px; --smush-placeholder-aspect-ratio: 480\/249;\" \/><\/p>\n<p>\u5728\u9879\u76ee\u6781\u5176\u590d\u6742\uff0c\u8fd0\u884c\u65f6\u6dfb\u52a0\u53c2\u6570\u8f83\u591a\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u522b\u540d\u53ef\u4ee5\u66f4\u65b9\u4fbf\u7684\u8fd0\u884c<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E5%8F%91%E5%B8%83%E5%8C%85\"><\/span>\u53d1\u5e03\u5305<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u5728 npm \u6e90\u7ad9\u6ce8\u518c\u4e00\u4e2a\u8d26\u53f7\uff1a<a href=\"https:\/\/www.npmjs.com\/\">https:\/\/www.npmjs.com\/<\/a><\/p>\n<p>\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6f14\u793a\u53d1\u5e03<br \/>\n\u521b\u5efa\u4e00\u4e2a\u5305\u540d\u4e3a iyatt-package<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713754462745.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 774px; --smush-placeholder-aspect-ratio: 774\/354;\" \/><\/p>\n<p>\u7f16\u5199\u6e90\u7801<br \/>\nindex.mjs<\/p>\n<pre><code class=\"language-js\">export function add(num1, num2) {\n    return num1 + num2;\n}<\/code><\/pre>\n<p>\u5982\u679c\u4fee\u6539\u8fc7 npm \u6e90\u7ad9\u7684\uff0c\u5728\u8fdb\u884c\u53d1\u5e03\u64cd\u4f5c\u7684\u65f6\u5019\u8981\u6362\u56de\u5b98\u65b9\u7684\u6e90\u7ad9\u624d\u884c\uff0c\u955c\u50cf\u7ad9\u4e0d\u652f\u6301\u53d1\u5e03\u5305\u3002<\/p>\n<p>npm \u767b\u5f55\u6ce8\u518c\u7684\u8d26\u53f7<\/p>\n<pre><code class=\"language-c\">npm login<\/code><\/pre>\n<p>\u53d1\u5e03<\/p>\n<pre><code class=\"language-c\">npm publish<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713754731133.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 642px; --smush-placeholder-aspect-ratio: 642\/375;\" \/><\/p>\n<p>\u5728 npm \u6e90\u7ad9\u4e0a\u5c31\u80fd\u641c\u5230\u4e86<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713754826544.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1554px; --smush-placeholder-aspect-ratio: 1554\/586;\" \/><\/p>\n<p>\u53ef\u4ee5\u6267\u884c\u547d\u4ee4\u4ece\u6e90\u7ad9\u4e0b\u8f7d\u5b89\u88c5\u8fd9\u4e2a\u5305<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713754884104.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 528px; --smush-placeholder-aspect-ratio: 528\/127;\" \/><\/p>\n<p>\u5199\u4e00\u6bb5\u4ee3\u7801\u6d4b\u8bd5\u5305\u8c03\u7528<\/p>\n<pre><code class=\"language-js\">import { add } from &#039;iyatt-package&#039;;\n\nconsole.log(add(1, 2));<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713755090826.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 416px; --smush-placeholder-aspect-ratio: 416\/109;\" \/><\/p>\n<p>\u5982\u679c\u540e\u9762\u8981\u53d1\u5e03\u65b0\u7248\u672c\u7684\u5305\uff0c\u628a package.json \u91cc\u7684\u7248\u672c\u6539\u4e00\u4e0b\uff0c\u518d\u6267\u884c\u53d1\u5e03\u547d\u4ee4\u5c31\u53ef\u4ee5\u3002<br \/>\n\u5982\u679c\u8981\u5220\u9664\u53d1\u5e03\u7684\u5305\u53ef\u4ee5\u5230 npm \u6e90\u7ad9\u4e0a\u64cd\u4f5c\uff0c\u66f4\u4e3a\u65b9\u4fbf\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86\"><\/span>\u7248\u672c\u7ba1\u7406<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u7528\u4e8e\u7ba1\u7406 Node.js \u7248\u672c\u7684\u5de5\u5177\u633a\u591a\u7684\uff0c\u6bd4\u5982 nvm \u548c n \u7b49\uff0c\u5176\u4e2d n \u4e0d\u652f\u6301 Windows\uff0cWindows \u4e0b\u63a8\u8350\u4f7f\u7528 nvm-windows: <a href=\"https:\/\/github.com\/coreybutler\/nvm-windows\">https:\/\/github.com\/coreybutler\/nvm-windows<\/a><\/p>\n<p>\u9700\u8981\u524d\u5f80\u9879\u76ee\u9875 Release \u4e0b\u8f7d\u5b89\u88c5\u5305\uff0c\u9879\u76ee\u9875\u4e0a\u6709\u4f7f\u7528\u8bf4\u660e\uff0c\u53ef\u4ee5\u7528\u4e8e\u5347\u7ea7 Node.js\uff0c\u5728\u591a\u4e2a\u7248\u672c\u4e4b\u95f4\u5207\u6362\u7b49\u7b49\u3002<\/p>\n<p>\u5982\u679c\u662f Linux \u53ef\u4ee5\u4f7f\u7528 n \u6765\u7ba1\u7406\uff0c\u5b89\u88c5\u4e5f\u65b9\u4fbf\uff0c\u76f4\u63a5\u4f7f\u7528 npm<\/p>\n<pre><code class=\"language-c\">npm i -g n<\/code><\/pre>\n<p>npm \u6e90\u7ad9\u4e0a\u6709 n \u547d\u4ee4\u7684\u4f7f\u7528\u8bf4\u660e\uff1a<a href=\"https:\/\/www.npmjs.com\/package\/n\">https:\/\/www.npmjs.com\/package\/n<\/a><\/p>\n<h1><span class=\"ez-toc-section\" id=\"express_%E6%A1%86%E6%9E%B6\"><\/span>express \u6846\u67b6<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Express \u6587\u6863\uff1a<a href=\"https:\/\/expressjs.com\/zh-cn\/starter\/installing.html\">https:\/\/expressjs.com\/zh-cn\/starter\/installing.html<\/a><br \/>\n\u57fa\u4e8e express 4.9.12 \u5b9e\u8df5\uff0c\u76ee\u524d 5.x \u7248\u672c\u8fd8\u6ca1\u6709\u51fa\u7a33\u5b9a\u7248<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713775869066.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1381px; --smush-placeholder-aspect-ratio: 1381\/1019;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"hello_world\"><\/span>hello world<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\n\nconst app = express();\napp.get(&#039;\/&#039;, (req, res) =&gt; {\n    res.send(&#039;Hello World!&#039;);\n});\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:80&#039;);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713776167584.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 280px; --smush-placeholder-aspect-ratio: 280\/134;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E8%B7%AF%E7%94%B1\"><\/span>\u8def\u7531<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u6587\u6863\uff1a<a href=\"https:\/\/expressjs.com\/zh-cn\/guide\/routing.html\">https:\/\/expressjs.com\/zh-cn\/guide\/routing.html<\/a><\/p>\n<p>\u7528\u4e8e\u6a21\u62df\u53d1\u9001 POST \u8bf7\u6c42\u7684 html<br \/>\nGET \u8bf7\u6c42\u901a\u5e38\u5c31\u662f\u76f4\u63a5\u8bbf\u95ee\u670d\u52a1\u5668\uff0c\u901a\u8fc7\u94fe\u63a5\u5411\u670d\u52a1\u5668\u4f20\u8f93\u4e00\u4e9b\u6570\u636e\u3002\u6bd4\u5982 <a href=\"http:\/\/localhost\/a?b=123&amp;c=098\">http:\/\/localhost\/a?b=123&#038;c=098<\/a> \u8bbf\u95ee\u8def\u5f84 a\uff0c\u4f20\u8f93 b=123 \u548c c=098 \u6570\u636e\uff0c\u7531\u4e8e URL \u957f\u5ea6\u9650\u5236\uff0c\u53ef\u4f20\u9001\u7684\u6570\u636e\u4e5f\u6709\u9650\u3002\u4e00\u822c\u7528\u4e8e\u5411\u670d\u52a1\u5668\u8bf7\u6c42\u6570\u636e\uff0c\u670d\u52a1\u5668\u6536\u5230\u540e\uff0c\u54cd\u5e94\u9700\u8981\u7684\u5185\u5bb9\u3002<br \/>\nPOST \u5219\u662f\u901a\u8fc7\u8bf7\u6c42\u4e3b\u4f53\u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\uff0c\u5728\u94fe\u63a5\u4e0a\u662f\u770b\u4e0d\u51fa\u6765\u7684\uff0c\u4e5f\u6ca1\u6709\u4f20\u8f93\u6570\u636e\u91cf\u7684\u9650\u5236\uff0c\u901a\u5e38\u7528\u4e8e\u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\u7684\u65f6\u5019\u4f7f\u7528\u3002<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n\n&lt;head&gt;\n    &lt;title&gt;\u53d1\u9001 POST&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n    &lt;form action=&quot;http:\/\/localhost\/post&quot; method=&quot;POST&quot;&gt;\n        &lt;textarea id=&quot;content&quot; name=&quot;content&quot; rows=&quot;5&quot; cols=&quot;30&quot; required&gt;&lt;\/textarea&gt;&lt;br&gt;&lt;br&gt;\n        &lt;input type=&quot;submit&quot; value=&quot;\u53d1\u9001&quot;&gt;\n    &lt;\/form&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E8%B7%AF%E7%94%B1%E6%96%B9%E6%B3%95\"><\/span>\u8def\u7531\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\n\nconst app = express();\n\n\/\/ GET \u65b9\u6cd5\u8def\u7531\uff1ahttp:\/\/localhost\/\napp.get(&#039;\/&#039;, (req, res) =&gt; {\n    res.send(&#039;\u8bbf\u95ee\u6839\u76ee\u5f55&#039;);\n});\n\n\/\/ POST \u65b9\u6cd5\u8def\u7531; http:\/\/localhost\/post\napp.post(&#039;\/post&#039;, (req, res) =&gt; {\n    res.send(&#039;\u53d1\u9001 POST \u8bf7\u6c42&#039;);\n});\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:80&#039;);\n});<\/code><\/pre>\n<p>\u8bbf\u95ee <a href=\"http:\/\/localhost\/\">http:\/\/localhost\/<\/a><br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713783949188.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 265px; --smush-placeholder-aspect-ratio: 265\/126;\" \/><\/p>\n<p>\u7528\u6d4f\u89c8\u5668\u6253\u5f00\u4e0a\u9762\u7528\u4e8e\u53d1 POST \u7684 html \u6587\u4ef6<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713784099944.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 366px; --smush-placeholder-aspect-ratio: 366\/248;\" \/><br \/>\n\u70b9\u51fb\u53d1\u9001\u4ee5\u540e\uff0c\u670d\u52a1\u5668\u7aef\u7531 POST \u8def\u7531\u5904\u7406\uff0c\u5e76\u54cd\u5e94\u5185\u5bb9<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713784261755.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 312px; --smush-placeholder-aspect-ratio: 312\/130;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E6%89%80%E6%9C%89%E6%96%B9%E6%B3%95\"><\/span>\u6240\u6709\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u4e0a\u9762\u7684\u662f\u9488\u5bf9 GET \u548c POST \u4f7f\u7528\u4e13\u7528\u7684\u65b9\u6cd5\uff0cexpress \u4e2d\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a all \u53ef\u4ee5\u5904\u7406\u6240\u6709\u7684 http \u65b9\u6cd5<\/p>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\n\nconst app = express();\n\napp.all(&#039;\/post&#039;, (req, res, next) =&gt; {\n    res.send(req.method);\n    next();\n});\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:80&#039;);\n});<\/code><\/pre>\n<p>\u53d1\u9001 POST \u8bf7\u6c42\u540e\u6536\u5230\u54cd\u5e94<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713785294376.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 272px; --smush-placeholder-aspect-ratio: 272\/122;\" \/><br \/>\n\u53d1\u9001 GET \u8bf7\u6c42\u548c\u6536\u5230\u54cd\u5e94<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713785316348.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 263px; --smush-placeholder-aspect-ratio: 263\/108;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E8%B7%AF%E7%94%B1%E8%B7%AF%E5%BE%84\"><\/span>\u8def\u7531\u8def\u5f84<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\n\nconst app = express();\n\napp.get(&#039;\/&#039;, (req, res) =&gt; {\n    res.send(&#039;root&#039;);\n});\n\napp.get(&#039;\/about&#039;, (req, res) =&gt; {\n    res.send(&#039;about&#039;);\n});\n\n\/\/ \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8def\u5f84\uff0c\u53ef\u5339\u914d abcd\u3001abbcd\u3001abbbcd......\napp.get(&#039;\/ab+cd&#039;, (req, res) =&gt; {\n    res.send(&#039;ab+cd&#039;);\n});\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:80&#039;);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713788091501.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 225px; --smush-placeholder-aspect-ratio: 225\/125;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713788115503.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 305px; --smush-placeholder-aspect-ratio: 305\/114;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713788127774.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 320px; --smush-placeholder-aspect-ratio: 320\/123;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713788141093.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 305px; --smush-placeholder-aspect-ratio: 305\/145;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E8%B7%AF%E7%94%B1%E5%8F%82%E6%95%B0\"><\/span>\u8def\u7531\u53c2\u6570<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u901a\u8fc7\u94fe\u63a5\u4f20\u9012\u7684\u53c2\u6570\u53ef\u4ee5\u63d0\u53d6\u51fa\u6765<\/p>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\n\nconst app = express();\n\n\/\/ \u5192\u53f7\u540e\u662f\u8981\u5339\u914d\u7684\u53c2\u6570\napp.get(&#039;\/user\/:user\/password\/:password&#039;, (req, res) =&gt; {\n    res.send(&#039;\u7528\u6237\u540d\uff1a&#039; + req.params.user + &#039; \u5bc6\u7801\uff1a&#039; + req.params.password); \/\/ \u63d0\u53d6\u53c2\u6570\n});\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:80&#039;);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713789074922.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 446px; --smush-placeholder-aspect-ratio: 446\/117;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E8%B7%AF%E7%94%B1%E6%A8%A1%E5%9D%97%E5%8C%96\"><\/span>\u8def\u7531\u6a21\u5757\u5316<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>homeRouter.mjs<\/p>\n<pre><code class=\"language-js\">import { Router } from &#039;express&#039;;\n\nconst homeRouter = Router();\n\nhomeRouter.get(&#039;\/&#039;, (req, res) =&gt; {\n    res.send(&#039;root&#039;);\n});\n\nexport default homeRouter;<\/code><\/pre>\n<p>adminRouter.mjs<\/p>\n<pre><code class=\"language-js\">import { Router } from &quot;express&quot;;\n\nconst adminRouter = Router();\n\nadminRouter.get(&#039;\/admin&#039;, (req, res) =&gt; {\n    res.send(&#039;admin&#039;);\n});\n\nexport default adminRouter;<\/code><\/pre>\n<p>index.mjs<\/p>\n<pre><code class=\"language-js\">import homeRouter from &quot;.\/homeRouter.mjs&quot;;\nimport adminRouter from &quot;.\/adminRouter.mjs&quot;;\nimport express from &quot;express&quot;;\n\nconst app = express();\n\napp.use(homeRouter);\napp.use(adminRouter);\n\napp.all(&#039;*&#039;, (req, res) =&gt; {\n    res.status(404).send(&#039;Page not found&#039;);\n});\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server started on http:\/\/localhost:80&#039;);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713872562578.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 255px; --smush-placeholder-aspect-ratio: 255\/132;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713872576103.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 282px; --smush-placeholder-aspect-ratio: 282\/119;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713872586899.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 290px; --smush-placeholder-aspect-ratio: 290\/111;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E4%B8%AD%E9%97%B4%E4%BB%B6\"><\/span>\u4e2d\u95f4\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u6587\u6863\uff1a<\/p>\n<ul>\n<li>\u5199\u4e2d\u95f4\u4ef6\uff1a<a href=\"https:\/\/expressjs.com\/zh-cn\/guide\/writing-middleware.html\">https:\/\/expressjs.com\/zh-cn\/guide\/writing-middleware.html<\/a><\/li>\n<li>\u4f7f\u7528\u4e2d\u95f4\u95f4\uff1a<a href=\"https:\/\/expressjs.com\/zh-cn\/guide\/using-middleware.html\">https:\/\/expressjs.com\/zh-cn\/guide\/using-middleware.html<\/a><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"%E5%85%A8%E5%B1%80%E4%B8%AD%E9%97%B4%E4%BB%B6\"><\/span>\u5168\u5c40\u4e2d\u95f4\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u8fd9\u91cc\u5199\u4e86\u4e00\u4e2a\u8bb0\u5f55\u8bbf\u95ee\u65e5\u5fd7\u7684\u529f\u80fd\uff0c\u8981\u5904\u7406\u7684\u8def\u7531\u6709\u591a\u4e2a\uff0c\u800c\u8bbf\u95ee\u8bb0\u5f55\u662f\u8bb0\u5f55\u6240\u6709\u8def\u7531\u63a5\u6536\u8bf7\u6c42\u7684\u8bb0\u5f55\uff0c\u7b49\u4e8e\u6240\u6709\u8def\u7531\u90fd\u9700\u8981\u6267\u884c\u540c\u4e00\u5957\u4ee3\u7801\u3002\u90a3\u4e48\u53ef\u4ee5\u628a\u65e5\u5fd7\u8bb0\u5f55\u90e8\u5206\u5355\u72ec\u62bd\u51fa\u6765\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u5168\u5c40\u4e2d\u95f4\u4ef6\u3002\u4e2d\u95f4\u4ef6\u4f1a\u5728\u8def\u7531\u4e2d\u8bed\u53e5\u6267\u884c\u4e4b\u524d\u5148\u6267\u884c\uff0c\u6240\u4ee5\u4e2d\u95f4\u4ef6\u4e2d\u8981\u5199\u4e2a next()\uff0c\u7528\u4e8e\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff0c\u5426\u5219\u5728\u4e2d\u95f4\u4ef6\u6267\u884c\u540e\uff0c\u5c31\u4e0d\u4f1a\u518d\u6267\u884c\u8def\u7531\u4e2d\u7684\u8bed\u53e5\u3002<\/p>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\nimport { appendFileSync } from &#039;node:fs&#039;;\nimport { resolve } from &#039;node:path&#039;;\n\nconst accessLogPath = resolve(&#039;access.log&#039;);\nconst app = express();\n\n\/\/ \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\nfunction getTime() {\n    const now = new Date();\n    const year = now.getFullYear();\n    const month = now.getMonth() + 1;\n    const day = now.getDate();\n    const hour = now.getHours();\n    const minute = now.getMinutes();\n    const second = now.getSeconds();\n    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;\n}\n\n\/\/ \u5b9a\u4e49\u4e2d\u95f4\u4ef6\uff0c\u7528\u4e8e\u8bb0\u5f55\u8bbf\u95ee\u65e5\u5fd7\nfunction recordAccessLog(req, res, next) {\n    const { url, method, ip, httpVersion } = req;\n    appendFileSync(accessLogPath, `${getTime()} ${method} ${url} ${ip} ${httpVersion}\\n`, &#039;utf-8&#039;);\n    next();\n}\n\n\/\/ \u4f7f\u7528\u4e2d\u95f4\u4ef6\napp.use(recordAccessLog);\n\napp.get(&#039;\/&#039;, (req, res) =&gt; {\n    res.send(&#039;root&#039;);\n});\n\napp.get(&#039;\/home&#039;, (req, res) =&gt; {\n    res.send(&#039;home&#039;);\n});\n\napp.get(&#039;\/about&#039;, (req, res) =&gt; {\n    res.send(&#039;about&#039;);\n});\n\napp.all(&#039;*&#039;, (req, res) =&gt; {\n    res.status(404).send(&#039;404 Not Found&#039;);\n});\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:80&#039;);\n})<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713801520614.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 448px; --smush-placeholder-aspect-ratio: 448\/143;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E4%B8%AD%E9%97%B4%E4%BB%B6\"><\/span>\u9759\u6001\u8d44\u6e90\u4e2d\u95f4\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u524d\u9762\u8bd5\u8fc7\u4e0d\u4f9d\u8d56\u7b2c\u4e09\u65b9\u6a21\u5757\u7684\u9759\u6001\u8d44\u6e90\u7ad9\u5b9e\u73b0\uff1a<a href=\"https:\/\/blog.iyatt.com\/?p=14717#%E9%83%A8%E7%BD%B2%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E7%AB%99\">https:\/\/blog.iyatt.com\/?p=14717#%E9%83%A8%E7%BD%B2%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E7%AB%99<\/a><br \/>\n\u8fd9\u91cc\u91c7\u7528 express \u5b9e\u73b0\uff0c\u6781\u5927\u7b80\u5316\u4ee3\u7801\uff0c\u53ea\u9700\u8981\u4f20\u5165\u9759\u6001\u8d44\u6e90\u7684\u8def\u5f84\u5373\u53ef<br \/>\n\u6587\u6863\uff1a<a href=\"https:\/\/expressjs.com\/zh-cn\/starter\/static-files.html\">https:\/\/expressjs.com\/zh-cn\/starter\/static-files.html<\/a><\/p>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\nimport { resolve } from &#039;node:path&#039;;\n\nconst root = resolve(&#039;homepage&#039;);\nconst app = express();\n\napp.use(express.static(root));\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:80&#039;);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713836574292.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1017px; --smush-placeholder-aspect-ratio: 1017\/973;\" \/><\/p>\n<p>\u5728\u8bbf\u95ee <a href=\"http:\/\/localhost\/\">http:\/\/localhost\/<\/a> \u7684\u65f6\u5019\u6ca1\u6709\u6307\u5b9a\u8bbf\u95ee\u8def\u5f84\uff0c\u9ed8\u8ba4\u5c31\u4f1a\u52a0\u8f7d <a href=\"http:\/\/localhost\/index.html\">http:\/\/localhost\/index.html<\/a><br \/>\n\u5982\u679c\u5728\u67d0\u79cd\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5355\u72ec\u6307\u5b9a <a href=\"http:\/\/localhost\/\">http:\/\/localhost\/<\/a> \u8bbf\u95ee\u7684\u5185\u5bb9\uff0c\u5c31\u4f1a\u5b58\u5728\u95ee\u9898\u3002\u9759\u6001\u8d44\u6e90\u4e2d\u95f4\u4ef6\u662f\u4f5c\u4e3a\u5168\u5c40\u4e2d\u95f4\u4ef6\u4f7f\u7528\u7684\uff0c\u5b83\u4f1a\u5728\u8def\u7531\u4e4b\u524d\u6267\u884c\uff0c\u6240\u4ee5\u8bbf\u95ee <a href=\"http:\/\/localhost\/\">http:\/\/localhost\/<\/a> \u4f1a\u76f4\u63a5\u8fd4\u56de <a href=\"http:\/\/localhost\/index.html\">http:\/\/localhost\/index.html<\/a> \u7684\u5185\u5bb9\uff0c\u800c\u4e0d\u662f\u8def\u7531\u6307\u5b9a\u7684\u5185\u5bb9\u3002<br \/>\n\u8fd9\u4e2a\u5176\u5b9e\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u987a\u5e8f\u89e3\u51b3\uff0c\u628a\u8def\u7531\u90e8\u5206\u653e\u5230\u4f7f\u7528\u4e2d\u95f4\u4ef6\u7684\u524d\u9762\uff0c\u8fd9\u6837\u5c31\u4f1a\u6309\u7167\u8def\u7531\u7684\u65b9\u5f0f\u5904\u7406\u3002<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713837228930.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 276px; --smush-placeholder-aspect-ratio: 276\/155;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E9%98%B2%E7%9B%97%E9%93%BE\"><\/span>\u9632\u76d7\u94fe<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u9632\u6b62\u522b\u4eba\u5077\u7528\u4f60\u7684\u94fe\u63a5\u6302\u5230\u81ea\u5df1\u7684\u7f51\u7ad9\u4e0a\uff0c\u8fd9\u6837\u6bcf\u6b21\u8bbf\u95ee\u8d44\u6e90\u90fd\u4f1a\u5230\u81ea\u5df1\u7684\u670d\u52a1\u5668\u4e0a\u6765\u8bf7\u6c42\u8d44\u6e90\uff0c\u589e\u5927\u81ea\u5df1\u7684\u670d\u52a1\u5668\u538b\u529b\u3002<br \/>\n\u6d4f\u89c8\u5668\u5728\u8bf7\u6c42\u8d44\u6e90\u6587\u4ef6\u7684\u65f6\u5019\u4f1a\u643a\u5e26\u4e00\u4e2a referer \u5b57\u6bb5\uff0c\u53ef\u4ee5\u6307\u51fa\u662f\u4ece\u54ea\u4e2a\u5730\u5740\u53d1\u51fa\u7684\u8bf7\u6c42\u3002\u90a3\u4e48\u9632\u76d7\u94fe\u5c31\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u5b57\u6bb5\u6765\u5224\u65ad\uff0c\u5982\u679c\u8fd9\u4e2a\u5b57\u6bb5\u7684\u57df\u540d\uff08\u6216IP\uff09\u4e0e\u81ea\u5df1\u7684\u4e0d\u5339\u914d\uff0c\u90a3\u4e48\u5c31\u80fd\u5224\u65ad\u662f\u76d7\u94fe\uff0c\u7981\u6b62\u8bbf\u95ee\u3002<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713863757540.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 921px; --smush-placeholder-aspect-ratio: 921\/675;\" \/><\/p>\n<p>\u6cbf\u7528\u4e0a\u9762\u9759\u6001\u8d44\u6e90\u7ad9\u7684\u6e90\u7801\u8fdb\u884c\u5b9e\u73b0\uff1a<a href=\"https:\/\/blog.iyatt.com\/?p=14947#%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E4%B8%AD%E9%97%B4%E4%BB%B6\">https:\/\/blog.iyatt.com\/?p=14947#%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E4%B8%AD%E9%97%B4%E4%BB%B6<\/a><\/p>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\nimport { resolve } from &#039;node:path&#039;;\n\nconst root = resolve(&#039;homepage&#039;);\nconst app = express();\nconst serverName = &#039;localhost&#039;; \/\/ \u81ea\u5df1\u7684\u670d\u52a1\u5668\u5730\u5740\n\n\/\/ \u9632\u76d7\u94fe\napp.use( (req, res, next) =&gt; {\n    const referer = req.get(&#039;Referer&#039;);\n    if (referer) {\n        const url = new URL(referer);\n        const hostname = url.hostname;\n        if (hostname !== serverName) { \/\/ \u5982\u679c referer \u7684\u5730\u5740\u548c\u81ea\u5df1\u7684\u670d\u52a1\u5668\u5730\u5740\u4e0d\u4e00\u6837\u5c31\u7981\u6b62\u8bbf\u95ee\n            res.status(403).send(&#039;Forbidden&#039;);\n            return;\n        }\n    }\n    next();\n});\n\napp.use(express.static(root));\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:80&#039;);\n});<\/code><\/pre>\n<p>127.0.0.1 \u90fd\u662f\u672c\u5730\u56de\u73af\u5730\u5740\uff0clocalhost \u662f\u4f1a\u89e3\u6790\u4e3a 127.0.0.1 \u7684<\/p>\n<p>\u6b63\u5e38\u8bbf\u95ee<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713863970540.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 998px; --smush-placeholder-aspect-ratio: 998\/999;\" \/><br \/>\n\u91c7\u7528 127.0.0.1 \u8bbf\u95ee\uff0c\u867d\u7136\u672c\u8d28\u4e0a\u662f\u540c\u4e00\u4e2a\u6307\u5411\uff0c\u4f46\u662f\u56e0\u4e3a\u8bbf\u95ee\u5730\u5740\u4e0d\u540c\uff0c\u8d44\u6e90\u6587\u4ef6\u4f1a\u88ab\u7981\u6b62\u52a0\u8f7d<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713864100450.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 736px; --smush-placeholder-aspect-ratio: 736\/979;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E8%B7%AF%E7%94%B1%E4%B8%AD%E9%97%B4%E4%BB%B6\"><\/span>\u8def\u7531\u4e2d\u95f4\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u8def\u7531\u4e2d\u95f4\u4ef6\u7528\u4e8e\u6267\u884c\u6307\u5b9a\u8def\u7531\u7684\u64cd\u4f5c\u3002\u6bd4\u5982\uff0c\u4e00\u4e2a\u7f51\u7ad9\u7684\u524d\u53f0\u6b63\u5e38\u5c55\u793a\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5904\u7406\uff0c\u800c\u540e\u53f0\u7ba1\u7406\u548c\u8bbe\u7f6e\u4e5f\u8981\u8981\u9a8c\u8bc1\u767b\u5f55\u72b6\u6001\u624d\u80fd\u6253\u5f00\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u8def\u7531\u4e2d\u95f4\u4ef6\u5904\u7406\u3002<\/p>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\n\nconst app = express();\n\nfunction check(req, res, next) {\n    if (req.query.code === &#039;123&#039;) {\n        next();\n    }\n    else {\n        res.status(403).send(&#039;Forbidden&#039;);\n    }\n}\n\n\/\/ \u8def\u7531\u4e2d\u95f4\u4ef6\napp.get(&#039;\/&#039;, (req, res) =&gt; {\n    res.send(&#039;root&#039;);\n});\n\napp.get(&#039;\/admin&#039;, check, (req, res) =&gt; { \/\/ \u5728\u9700\u8981\u4f7f\u7528\u7684\u5730\u65b9\u52a0\u4e0a\u8def\u7531\u4e2d\u95f4\u4ef6\n    res.send(&#039;admin&#039;);\n});\n\napp.get(&#039;\/set&#039;, check, (req, res) =&gt; { \/\/ \u5728\u9700\u8981\u4f7f\u7528\u7684\u5730\u65b9\u52a0\u4e0a\u8def\u7531\u4e2d\u95f4\u4ef6\n    res.send(&#039;set&#039;);\n});\n\napp.all(&#039;*&#039;, (req, res) =&gt; {\n    res.status(404).send(&#039;404 Not Found&#039;);\n});\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:80&#039;);\n})<\/code><\/pre>\n<p>\u8bbf\u95ee\u524d\u53f0<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713835018748.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 269px; --smush-placeholder-aspect-ratio: 269\/105;\" \/><\/p>\n<p>\u8bbf\u95ee\u7ba1\u7406\u9875\u9762\uff0c\u6ca1\u6709\u767b\u5f55\u4fe1\u606f<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713835061233.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 284px; --smush-placeholder-aspect-ratio: 284\/114;\" \/><br \/>\n\u6709\u6b63\u786e\u7684\u767b\u5f55\u4fe1\u606f<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713835082629.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 367px; --smush-placeholder-aspect-ratio: 367\/128;\" \/><\/p>\n<p>\u8bbf\u95ee\u8bbe\u7f6e\u9875\u9762\uff0c\u767b\u5f55\u4fe1\u606f\u4e0d\u5339\u914d<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713835121792.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 333px; --smush-placeholder-aspect-ratio: 333\/114;\" \/><br \/>\n\u6b63\u786e\u7684\u767b\u5f55\u4fe1\u606f<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713835102398.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 352px; --smush-placeholder-aspect-ratio: 352\/105;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E8%8E%B7%E5%8F%96%E8%AF%B7%E6%B1%82%E4%BD%93%E5%86%85%E5%AE%B9\"><\/span>\u83b7\u53d6\u8bf7\u6c42\u4f53\u5185\u5bb9<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u4f7f\u7528\u4e2d\u95f4\u4ef6 urlencoded \u89e3\u7801\uff1a<a href=\"https:\/\/expressjs.com\/zh-cn\/api.html#express.urlencoded\">https:\/\/expressjs.com\/zh-cn\/api.html#express.urlencoded<\/a><br \/>\n\u8fd9\u91cc\u7528\u5230\u524d\u9762\u53d1 POST \u7684 html\uff1a<a href=\"https:\/\/blog.iyatt.com\/?p=14947#%E8%B7%AF%E7%94%B1\">https:\/\/blog.iyatt.com\/?p=14947#%E8%B7%AF%E7%94%B1<\/a><\/p>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\nimport { urlencoded } from &#039;express&#039;;\nimport { resolve } from &#039;node:path&#039;;\n\nconst app = express();\nconst up = urlencoded();\n\n\/\/ \u54cd\u5e94 html \u9875\u9762\napp.get(&#039;\/post&#039;, (req, res) =&gt; {\n    res.sendFile(resolve(&#039;index.html&#039;));\n});\n\n\/\/ \u54cd\u5e94 POST \u8bf7\u6c42\napp.post(&#039;\/post&#039;, up, (req, res) =&gt; {\n    console.log(req.body.content);\n    res.send(&#039;\u5df2\u6536\u5230 POST&#039;);\n});\n\napp.listen(80, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:80&#039;);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713859317429.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 458px; --smush-placeholder-aspect-ratio: 458\/251;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713859364764.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 432px; --smush-placeholder-aspect-ratio: 432\/122;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1713859375164.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 442px; --smush-placeholder-aspect-ratio: 442\/173;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E_ejs\"><\/span>\u6a21\u677f\u5f15\u64ce ejs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u6587\u6863\uff1a<a href=\"https:\/\/ejs.co\/#docs\">https:\/\/ejs.co\/#docs<\/a><br \/>\n\u57fa\u4e8e 3.1.10 \u5b9e\u8df5<\/p>\n<p>\u5b89\u88c5<\/p>\n<pre><code class=\"language-c\">npm i ejs<\/code><\/pre>\n<p>ejs \u7684\u6548\u679c\u611f\u89c9\u548c <a href=\"https:\/\/blog.iyatt.com\/?p=15092#%E6%A8%A1%E6%9D%BF%E8%AF%AD%E6%B3%95\">Vue \u4e2d\u7684\u6a21\u677f\u8bed\u6cd5<\/a>\u5dee\u4e0d\u591a\u3002ejs \u901a\u5e38\u5728\u524d\u540e\u7aef\u4e0d\u5206\u79bb\u6216\u8005\u670d\u52a1\u7aef\u6e32\u67d3\u7684\u573a\u666f\u4e2d\u4f7f\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0 html \u548c js \u7684\u5206\u79bb\uff0c\u52a8\u6001\u4fee\u6539 html \u5185\u5bb9\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E4%BD%93%E9%AA%8C\"><\/span>\u4f53\u9a8c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>index.js<\/p>\n<pre><code class=\"language-js\">import ejs from &#039;ejs&#039;;\n\nconst value1 = &#039;\u4e2d\u56fd&#039;;\n\n\/\/ \u5c06\u5b57\u7b26\u4e32\u4e2d\u7684 label \u66ff\u6362\u4e3a value1 \u7684\u5185\u5bb9\nconst result1 = ejs.render(&#039;\u4f60\u597d\uff0c &lt;%= label %&gt;&#039;, { label: value1});\nconsole.log(result1);\n\n\/\/ \u7b80\u5199\nconst result2 = ejs.render(&#039;\u4f60\u597d\uff0c &lt;%= value1 %&gt;&#039;, { value1 });\nconsole.log(result2);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714140642615.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 381px; --smush-placeholder-aspect-ratio: 381\/114;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"html_%E6%96%87%E6%9C%AC%E6%8F%92%E5%80%BC\"><\/span>html \u6587\u672c\u63d2\u503c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>index.html<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n    &lt;head&gt;\n        &lt;meta charset=&quot;UTF-8&quot; \/&gt;\n        &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; \/&gt;\n        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; \/&gt;\n        &lt;title&gt;Document&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;h1&gt; \u4f60\u597d &lt;%= value1 %&gt; &lt;\/h1&gt;\n        &lt;h2&gt; &lt;%= value2 %&gt; &lt;\/h2&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>index.js<\/p>\n<pre><code class=\"language-js\">import ejs from &#039;ejs&#039;;\nimport { readFileSync } from &#039;fs&#039;;\n\nconst html = readFileSync(&#039;index.html&#039;).toString();\nconst value1 = &#039;\u4e2d\u56fd&#039;;\nconst value2 = &#039;\u8d70\u5411\u4e16\u754c&#039;;\n\nconst result = ejs.render(html, { value1, value2});\nconsole.log(result);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714141589757.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 661px; --smush-placeholder-aspect-ratio: 661\/308;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E5%88%97%E8%A1%A8%E6%B8%B2%E6%9F%93\"><\/span>\u5217\u8868\u6e32\u67d3<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>index.html<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot; \/&gt;\n    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; \/&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; \/&gt;\n    &lt;title&gt;\u6c34\u679c&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n    &lt;ul&gt;\n        &lt;% values.forEach(item=&gt; { %&gt;\n        &lt;li&gt;\n            &lt;%= item %&gt;\n        &lt;\/li&gt;\n        &lt;% }) %&gt;\n    &lt;\/ul&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/code><\/pre>\n<p>index.js<\/p>\n<pre><code class=\"language-js\">import ejs from &#039;ejs&#039;;\nimport { readFileSync } from &#039;fs&#039;;\n\nconst values = [&#039;\u82f9\u679c&#039;, &#039;\u9999\u8549&#039;, &#039;\u68a8&#039;, &#039;\u897f\u74dc&#039;];\nconst html = readFileSync(&#039;index.html&#039;).toString();\n\nconst result = ejs.render(html, { values });\nconsole.log(result);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714142452580.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 278px; --smush-placeholder-aspect-ratio: 278\/514;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%9D%A1%E4%BB%B6%E6%B8%B2%E6%9F%93\"><\/span>\u6761\u4ef6\u6e32\u67d3<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>index.html<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot; \/&gt;\n    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; \/&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; \/&gt;\n    &lt;title&gt;\u6c34\u679c&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n    &lt;header&gt;\n        &lt;% if (flag) { %&gt;\n        &lt;span&gt;\u4f60\u597d&lt;\/span&gt;}\n        &lt;% } else { %&gt;\n        &lt;button&gt; \u5f00\u59cb &lt;\/button&gt;\n        &lt;% } %&gt;\n    &lt;\/header&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/code><\/pre>\n<p>index.js<\/p>\n<pre><code class=\"language-js\">import ejs from &#039;ejs&#039;;\nimport { readFileSync } from &#039;fs&#039;;\n\nconst flag = true;\nconst html = readFileSync(&#039;index.html&#039;).toString();\n\nconst result = ejs.render(html, { flag });\nconsole.log(result);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714149998336.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 355px; --smush-placeholder-aspect-ratio: 355\/154;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E5%9C%A8_express_%E4%B8%AD%E4%BD%BF%E7%94%A8_ejs\"><\/span>\u5728 express \u4e2d\u4f7f\u7528 ejs<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u6587\u4ef6\u7ed3\u6784<\/p>\n<pre><code class=\"language-c\">\u2500\u2500index.js\n\u2502\n\u2514\u2500views\n        home.ejs<\/code><\/pre>\n<p>\u6a21\u677f\u6587\u4ef6\u6269\u5c55\u540d\u4f7f\u7528 .ejs<\/p>\n<p>home.ejs<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot; \/&gt;\n    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; \/&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; \/&gt;\n    &lt;title&gt;home&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n    &lt;h1&gt;&lt;%= title %&gt;&lt;\/h1&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/code><\/pre>\n<p>index.js<\/p>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\nimport { resolve } from &#039;node:path&#039;;\n\nconst app = express();\n\napp.set(&#039;view engine&#039;, &#039;ejs&#039;); \/\/ \u8bbe\u7f6e\u6a21\u677f\u5f15\u64ce\napp.set(&#039;views&#039;, resolve(&#039;views&#039;)); \/\/ \u8bbe\u7f6e\u6a21\u677f\u6587\u4ef6\u5b58\u653e\u7684\u76ee\u5f55\n\napp.get(&#039;\/&#039;, (req, res) =&gt; {\n    const title = &#039;IYATT-yx&#039;;\n    res.render(&#039;home&#039;, { title }); \/\/ \u6e32\u67d3\u6a21\u677f\u6587\u4ef6 home.ejs\n});\n\napp.listen(80, () =&gt;\n    console.log(&#039;\u670d\u52a1\u5668\u5df2\u542f\u52a8\uff0c\u8bbf\u95ee http:\/\/localhost\/&#039;)\n);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714150827168.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 259px; --smush-placeholder-aspect-ratio: 259\/168;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0\"><\/span>\u6587\u4ef6\u4e0a\u4f20<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4f7f\u7528 formidable \u6765\u5904\u7406\u8bf7\u6c42\u5185\u5bb9<br \/>\n\u57fa\u4e8e 3.5.1 \u5b9e\u8df5<br \/>\n\u8bf4\u660e\uff1a<a href=\"https:\/\/www.npmjs.com\/package\/formidable?activeTab=readme\">https:\/\/www.npmjs.com\/package\/formidable?activeTab=readme<\/a><\/p>\n<pre><code class=\"language-c\">npm i formidable<\/code><\/pre>\n<p>\u76ee\u5f55\u7ed3\u6784<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714199232997.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 362px; --smush-placeholder-aspect-ratio: 362\/272;\" \/><\/p>\n<p>\u901a\u8fc7\u7f51\u9875\u4e0a\u4f20\u7684\u56fe\u7247\u4f1a\u4fdd\u5b58\u5230 public\/images \u4e0b<\/p>\n<p>\u5165\u53e3<br \/>\nindex.js<\/p>\n<pre><code class=\"language-js\">import router from &#039;.\/routes\/route.js&#039;;\nimport { rootDir } from &#039;.\/constants.js&#039;;\n\nimport express from &#039;express&#039;;\nimport { resolve } from &#039;node:path&#039;;\n\nconst app = express();\n\napp.set(&#039;view engine&#039;, &#039;ejs&#039;); \/\/ \u8bbe\u7f6e\u6a21\u677f\u5f15\u64ce\napp.set(&#039;views&#039;, resolve(rootDir, &#039;views&#039;)); \/\/ \u8bbe\u7f6e\u6a21\u677f\u6587\u4ef6\u5b58\u653e\u7684\u76ee\u5f55\n\napp.use(router)\napp.use(express.static(resolve(rootDir, &#039;..&#039;, &#039;public&#039;))); \/\/ \u9759\u6001\u8d44\u6e90\n\napp.listen(80, () =&gt;\n    console.log(&#039;\u670d\u52a1\u5668\u5df2\u542f\u52a8\uff0c\u8bbf\u95ee http:\/\/localhost\/&#039;)\n);<\/code><\/pre>\n<p>\u5e38\u91cf<br \/>\nconstants.js<\/p>\n<pre><code class=\"language-js\">import { fileURLToPath } from &#039;url&#039;;\nimport { dirname } from &#039;path&#039;;\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\nexport const rootDir = __dirname;\nexport const hostUrl = &#039;http:\/\/localhost\/&#039;;<\/code><\/pre>\n<p>\u8def\u7531<br \/>\nroute.js<\/p>\n<pre><code class=\"language-js\">import { rootDir, hostUrl } from &quot;..\/constants.js&quot;\n\nimport { Router } from &quot;express&quot;;\nimport formidable from &#039;formidable&#039;;\nimport { resolve } from &#039;node:path&#039;;\n\nconst router = Router();\n\nconst form = formidable({\n    multiples: true, \/\/ \u662f\u5426\u652f\u6301\u591a\u6587\u4ef6\u4e0a\u4f20\n    uploadDir: resolve(rootDir, &#039;..&#039;, &#039;public&#039;, &#039;images&#039;), \/\/ \u4e0a\u4f20\u6587\u4ef6\u7684\u4fdd\u5b58\u8def\u5f84\n    keepExtensions: true, \/\/ \u4fdd\u7559\u6269\u5c55\u540d\n});\n\nrouter.get(&#039;\/&#039;, (req, res) =&gt; {\n    res.render(&#039;view&#039;); \/\/ \u6e32\u67d3\u6a21\u677f\u6587\u4ef6 home.ejs\n});\n\nrouter.post(&#039;\/api\/upload&#039;, (req, res, next) =&gt; {\n    \/\/ \u8bf7\u6c42\u6570\u636e\u5904\u7406\n    form.parse(req, (err, fields, files) =&gt; {\n        if (err) {\n            next(err);\n            return;\n        }\n\n        \/\/ \u62fc\u63a5\u8bbf\u95ee\u94fe\u63a5\n        const urls = [];\n        files.files.forEach( (file) =&gt; {\n            urls.push(hostUrl + &#039;images\/&#039; + file.newFilename);\n        });\n\n        \/\/ \u54cd\u5e94\n        res.json({\n            fields, files,\n            &quot;\u8bbf\u95ee\u94fe\u63a5&quot;: urls\n        });\n    });\n});\n\nexport default router;<\/code><\/pre>\n<p>\u6a21\u677f<br \/>\nview.ejs<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot; \/&gt;\n    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; \/&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; \/&gt;\n    &lt;title&gt;\u6587\u4ef6\u4e0a\u4f20&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n    &lt;form action=&quot;\/api\/upload&quot; method=&quot;post&quot; enctype=&quot;multipart\/form-data&quot;&gt;\n        \u7528\u6237\u540d\uff1a&lt;input type=&quot;text&quot; name=&quot;username&quot; \/&gt;&lt;br&gt;\n        \u6587\u4ef6\uff1a&lt;input type=&quot;file&quot; name=&quot;files&quot; multiple\/&gt;&lt;br&gt;\n        &lt;button&gt;\u63d0\u4ea4&lt;\/button&gt;\n    &lt;\/form&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/code><\/pre>\n<p>\u4e0a\u4f20\u5355\u5f20\u56fe\u7247<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714198374029.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 330px; --smush-placeholder-aspect-ratio: 330\/163;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714198408860.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1002px; --smush-placeholder-aspect-ratio: 1002\/409;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714198430110.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1269px; --smush-placeholder-aspect-ratio: 1269\/1093;\" \/><\/p>\n<p>\u4e0a\u4f20\u591a\u5f20\u56fe\u7247<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714198461665.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 345px; --smush-placeholder-aspect-ratio: 345\/179;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714198513717.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1007px; --smush-placeholder-aspect-ratio: 1007\/963;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714198547265.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 551px; --smush-placeholder-aspect-ratio: 551\/717;\" \/><\/p>\n<h1><span class=\"ez-toc-section\" id=\"MySQL_%E6%95%B0%E6%8D%AE%E5%BA%93\"><\/span>MySQL \u6570\u636e\u5e93<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u8fd9\u662f\u6211\u524d\u9762\u5b66\u4e60 MySQL \u7684\u8bb0\u5f55\uff1a<a href=\"https:\/\/blog.iyatt.com\/?p=12631\">https:\/\/blog.iyatt.com\/?p=12631<\/a><br \/>\n\u57fa\u4e8e MySQL 8.2.0 \u5b9e\u8df5<\/p>\n<p>\u8bd5\u4e86\u4e24\u4e2a\u9a71\u52a8<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E9%A9%B1%E5%8A%A81%EF%BC%9Amysql\"><\/span>\u9a71\u52a81\uff1amysql<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>mysql 2.18.1\uff0c\u6587\u6863\uff1a<a href=\"https:\/\/www.npmjs.com\/package\/mysql\">https:\/\/www.npmjs.com\/package\/mysql<\/a><\/p>\n<pre><code class=\"language-c\">npm i mysql@2.18.1<\/code><\/pre>\n<p>\u633a\u4e45\u6ca1\u66f4\u65b0\u4e86\uff0c\u4e0a\u4e00\u6b21\u66f4\u65b0\u8fd8\u662f 2020.1.24<br \/>\n\u53ef\u4ee5\u7528\uff0c\u4f46\u4e0d\u63a8\u8350<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714230975811.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 361px; --smush-placeholder-aspect-ratio: 361\/142;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E5%87%86%E5%A4%87\"><\/span>\u51c6\u5907<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93 test_db \u7528\u4e8e\u6d4b\u8bd5<\/p>\n<pre><code class=\"language-mysql\">CREATE DATABASE IF NOT EXISTS test_db;<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u5f20\u8868 test_tb<\/p>\n<pre><code class=\"language-mysql\">CREATE TABLE test_tb\n(\n    `id`         INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT &#039;primary key&#039;,\n    `bookName`   VARCHAR(256) DEFAULT NULL COMMENT &#039;book name&#039;,\n    `author`     VARCHAR(256) DEFAULT NULL COMMENT &#039;author&#039;\n);<\/code><\/pre>\n<p>\u63d2\u5165\u6570\u636e<\/p>\n<pre><code class=\"language-mysql\">INSERT INTO test_tb (bookName, author) VALUES\n(&#039;book1&#039;, &#039;author1&#039;),\n(&#039;book2&#039;, &#039;author2&#039;),\n(&#039;book3&#039;, &#039;author3&#039;);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714220576552.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 427px; --smush-placeholder-aspect-ratio: 427\/155;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93\"><\/span>\u8fde\u63a5\u6570\u636e\u5e93<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>index.js<\/p>\n<pre><code class=\"language-js\">import mysql from &#039;mysql&#039;;\n\nconst mysqlConfig = {\n    host: &#039;localhost&#039;, \/\/ \u6570\u636e\u5e93\u670d\u52a1\u5668\u5730\u5740\n    user: &#039;root&#039;, \/\/ \u6570\u636e\u5e93\u7528\u6237\u540d\n    password: &#039;1&#039;, \/\/ \u6570\u636e\u5e93\u5bc6\u7801\n    database: &#039;test_db&#039; \/\/ \u6570\u636e\u5e93\u540d\n};\n\nconst connection = mysql.createConnection(mysqlConfig);\n\n\/\/ \u8fde\u63a5\u6570\u636e\u5e93\nconnection.connect((err) =&gt; {\n    if (err) {\n        console.error(&#039;\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\uff1a&#039;, err);\n        return;\n    }\n    console.log(&#039;\u6570\u636e\u5e93\u8fde\u63a5\u6210\u529f&#039;);\n    console.log(&#039;connected as id &#039; + connection.threadId);\n});\n\n\/\/ \u67e5\u8be2\nconnection.query(&#039;SELECT * FROM test_tb&#039;, (err, results) =&gt; {\n    if (err) {\n        console.error(&#039;\u67e5\u8be2\u5931\u8d25\uff1a&#039;, err);\n        return;\n    }\n    results.forEach((row) =&gt; {\n        console.log(row);\n    });\n});\n\n\/\/ \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\nconnection.end((err) =&gt; {\n    if (err) {\n        console.error(&#039;\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\uff1a&#039;, err);\n        return;\n    }\n    console.log(&#039;\u6570\u636e\u5e93\u8fde\u63a5\u5df2\u5173\u95ed&#039;);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714221340809.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 477px; --smush-placeholder-aspect-ratio: 477\/176;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%9F%A5%E8%AF%A2\"><\/span>\u67e5\u8be2<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4><span class=\"ez-toc-section\" id=\"%E6%96%B9%E5%BC%8F%E4%B8%80\"><\/span>\u65b9\u5f0f\u4e00<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-js\">connection.query(&#039;SELECT * FROM test_tb WHERE author = &quot;author2&quot;&#039;, (err, results) =&gt; {\n    if (err) {\n        console.error(&#039;\u67e5\u8be2\u5931\u8d25\uff1a&#039;, err);\n        return;\n    }\n    results.forEach((row) =&gt; {\n        console.log(row);\n    });\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714221745017.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 520px; --smush-placeholder-aspect-ratio: 520\/152;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E6%96%B9%E5%BC%8F%E4%BA%8C\"><\/span>\u65b9\u5f0f\u4e8c<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-js\">\/\/ \u53ea\u6709\u4e00\u4e2a\u66ff\u6362\uff0c[&#039;author1&#039;] \u4e5f\u53ef\u4ee5\u5199\u4e3a &#039;author1&#039;\nconnection.query(&#039;SELECT * FROM test_tb WHERE author = ?&#039;, [&#039;author1&#039;], (err, results) =&gt; {\n    if (err) {\n        console.error(&#039;\u67e5\u8be2\u5931\u8d25\uff1a&#039;, err);\n        return;\n    }\n    results.forEach((row) =&gt; {\n        console.log(row);\n    });\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714221811026.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 477px; --smush-placeholder-aspect-ratio: 477\/155;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E6%96%B9%E5%BC%8F%E4%B8%89\"><\/span>\u65b9\u5f0f\u4e09<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-js\">const options = {\n    sql: &#039;SELECT * FROM test_tb WHERE author = ?&#039;,\n    timeout: 10000, \/\/ 10s\n    values: [&#039;author3&#039;]\n};\n\nconnection.query(options, (err, results) =&gt; {\n    if (err) {\n        console.error(&#039;\u67e5\u8be2\u5931\u8d25\uff1a&#039;, err);\n        return;\n    }\n    results.forEach((row) =&gt; {\n        console.log(row);\n    });\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714221964660.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 481px; --smush-placeholder-aspect-ratio: 481\/133;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E5%A2%9E%E5%88%A0%E6%94%B9\"><\/span>\u589e\u5220\u6539<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u589e\u5220\u6539\u7684\u64cd\u4f5c\u5728\u9a71\u52a8\u4f7f\u7528\u5c42\u9762\u548c\u67e5\u8be2\u90fd\u662f\u4e00\u6837\uff0c\u5b9e\u9645\u5177\u4f53\u884c\u4e3a\u4f9d\u8d56\u4e8e MySQL \u8bed\u53e5<\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E5%A2%9E\"><\/span>\u589e<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-js\">const options = {\n    sql: &#039;INSERT INTO test_tb (bookName, author) values (?, ?)&#039;,\n    values: [&#039;book4&#039;, &#039;author4&#039;]\n};\n\nconnection.query(options, (err, results) =&gt; {\n    if (err) {\n        console.error(&#039;\u67e5\u8be2\u5931\u8d25\uff1a&#039;, err);\n        return;\n    }\n    console.log(&#039;\u63d2\u5165\u6210\u529f&#039;);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714222686349.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 395px; --smush-placeholder-aspect-ratio: 395\/144;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714222705354.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 403px; --smush-placeholder-aspect-ratio: 403\/168;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E6%94%B9\"><\/span>\u6539<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-js\">const options = {\n    sql: &#039;UPDATE test_tb SET bookName = ? WHERE author = ?&#039;,\n    values: [&#039;bookbook4&#039;, &#039;author4&#039;]\n};\n\nconnection.query(options, (err, results) =&gt; {\n    if (err) {\n        console.error(&#039;\u67e5\u8be2\u5931\u8d25\uff1a&#039;, err);\n        return;\n    }\n    console.log(&#039;\u66f4\u65b0\u6210\u529f\uff0c\u53d7\u5f71\u54cd\u7684\u884c\u6570\uff1a&#039;, results.affectedRows);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714223117303.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 443px; --smush-placeholder-aspect-ratio: 443\/150;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714223132320.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 395px; --smush-placeholder-aspect-ratio: 395\/165;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E5%88%A0\"><\/span>\u5220<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-js\">const options = {\n    sql: &#039;DELETE FROM test_tb WHERE id = ?&#039;,\n    values: [1]\n};\n\nconnection.query(options, (err, results) =&gt; {\n    if (err) {\n        console.error(&#039;\u67e5\u8be2\u5931\u8d25\uff1a&#039;, err);\n        return;\n    }\n    console.log(&#039;\u5220\u9664\u6210\u529f&#039;);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714223274957.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 406px; --smush-placeholder-aspect-ratio: 406\/152;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714223343737.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 394px; --smush-placeholder-aspect-ratio: 394\/144;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E8%BD%AC%E4%B9%89%E6%9F%A5%E8%AF%A2\"><\/span>\u8f6c\u4e49\u67e5\u8be2<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u5177\u4f53\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\"https:\/\/www.npmjs.com\/package\/mysql#escaping-query-values\">https:\/\/www.npmjs.com\/package\/mysql#escaping-query-values<\/a><br \/>\n\u8f6c\u4e49\u67e5\u8be2\u7684\u610f\u4e49\u5728\u4e8e\u9632\u6b62 SQL \u6ce8\u5165\u3002<br \/>\n\u6bd4\u5982\u4e00\u4e2a\u767b\u5f55\u8868\u5355\uff0c\u540e\u7aef\u5199\u7684 \u201cSELECT <em> FROM users WHERE username = &#8216;[\u7528\u6237\u8f93\u5165]&#8217; AND password = &#8216;[\u7528\u6237\u8f93\u5165]&#8217;\u201d<br \/>\n\u7528\u6237\u4e0d\u8001\u5b9e\uff0c\u5728\u7528\u6237\u540d\u5904\u8f93\u5165\u4e86 \u201cadmin&#8217; &#8211;\u201d<br \/>\n\u90a3\u4e48\u5c31\u53d8\u6210\u4e86 \u201cSELECT <\/em> FROM users WHERE username = &#8216;admin&#8217; &#8211;&#8216; AND password = &#8216;[\u7528\u6237\u8f93\u5165]&#8217;\u201d<br \/>\n\u800c \u201c&#8211;\u201d \u5728 MySQL \u4e2d\u662f\u6ce8\u91ca\uff0c\u5bfc\u81f4\u5b9e\u9645\u53d8\u6210 \u201cSELECT * FROM users WHERE username = &#8216;admin&#8217;\u201d<br \/>\n\u539f\u672c\u6309\u7167\u8bbe\u8ba1\u662f\u8981\u68c0\u67e5\u7528\u6237\u540d\u548c\u5bc6\u7801\u540c\u65f6\u5339\u914d\u624d\u6267\u884c\u7684\uff0c\u73b0\u5728\u76f4\u63a5\u628a\u5bc6\u7801\u9a8c\u8bc1\u5ffd\u7565\u4e86\u3002<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u5982\u679c\u5bf9\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u8f6c\u4e49\u5904\u7406\uff0c\u5f97\u5230\u7684\u4f1a\u662f \u201cSELECT * FROM users WHERE username = &#8216;admin\\&#8217; &#8211;&#8216; AND password = &#8216;[\u7528\u6237\u8f93\u5165]&#8217;\u201d<br \/>\n\u7528\u6237\u8f93\u5165\u7684\u5355\u5f15\u53f7\u4e0d\u4f1a\u88ab\u89c6\u4e3a MySQL \u8bed\u53e5\u7684\u4e00\u90e8\u5206\uff0c\u800c\u662f\u4e00\u4e2a\u5355\u5f15\u53f7\u5b57\u7b26\uff0c\u5373\u5224\u65ad\u7528\u6237\u540d\u662f\u5426\u4e3a &quot;admin&#8217; &#8211;&quot;\uff0c\u6d88\u9664\u4e86\u5355\u5f15\u53f7\u7684\u5f71\u54cd\uff0c\u6ce8\u91ca\u7b26\u4e5f\u4e0d\u4f1a\u88ab\u4f5c\u4e3a MySQL \u8bed\u53e5\u7684\u4e00\u90e8\u5206\uff0c\u5bc6\u7801\u5224\u65ad\u4e5f\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E9%A9%B1%E5%8A%A82%EF%BC%9Amysql2\"><\/span>\u9a71\u52a82\uff1amysql2<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>mysql2 3.9.7\uff0c\u6587\u6863\uff1a<a href=\"https:\/\/sidorares.github.io\/node-mysql2\/docs\">https:\/\/sidorares.github.io\/node-mysql2\/docs<\/a><\/p>\n<pre><code class=\"language-c\">npm i mysql2@3.9.7<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E5%87%86%E5%A4%87-2\"><\/span>\u51c6\u5907<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u540c\u4e0a\uff1a<a href=\"https:\/\/blog.iyatt.com\/?p=14717#%E5%87%86%E5%A4%87\">https:\/\/blog.iyatt.com\/?p=14717#%E5%87%86%E5%A4%87<\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%9F%A5%E8%AF%A2-2\"><\/span>\u67e5\u8be2<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u8fd9\u4e2a\u9a71\u52a8\u7684\u9632 SQL \u6ce8\u5165\uff08\u65b9\u5f0f\u4e09\uff09\u539f\u7406\u548c <a href=\"https:\/\/blog.iyatt.com\/?p=14717#%E8%BD%AC%E4%B9%89%E6%9F%A5%E8%AF%A2\">mysql \u9a71\u52a8<\/a>\u4e0d\u4e00\u6837\u3002\u91c7\u7528\u9884\u5904\u7406\u7684\u65b9\u5f0f\uff0c\u63d0\u524d\u5c06\u6307\u4ee4\u90e8\u5206\u7f16\u8bd1\uff0c\u5360\u4f4d\u7b26\u7684\u4f4d\u7f6e\u662f\u7528\u4e8e\u586b\u5165\u7528\u6237\u8f93\u5165\u6570\u636e\u7684\uff0c\u4e00\u5f00\u59cb\u5c31\u628a\u6307\u4ee4\u548c\u6570\u636e\u5206\u79bb\u4e86\u3002\u6307\u4ee4\u53ea\u80fd\u662f\u4ee3\u7801\u4e2d\u5199\u5165\u7684\u90e8\u5206\uff0c\u7528\u6237\u4f20\u5165\u7684\u90e8\u5206\u53ea\u80fd\u662f\u6570\u636e\uff0c\u5c31\u4e0d\u4f1a\u5b58\u5728\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u88ab\u5f53\u4f5c\u6307\u4ee4\u7684\u4e00\u90e8\u5206\u5bfc\u81f4 SQL \u6ce8\u5165\u3002<\/p>\n<pre><code class=\"language-js\">import mysql from &#039;mysql2\/promise&#039;;\n\nconst mysqlConfig = {\n    host: &#039;localhost&#039;, \/\/ \u6570\u636e\u5e93\u670d\u52a1\u5668\u5730\u5740\n    user: &#039;root&#039;, \/\/ \u6570\u636e\u5e93\u7528\u6237\u540d\n    password: &#039;1&#039;, \/\/ \u6570\u636e\u5e93\u5bc6\u7801\n    database: &#039;test_db&#039; \/\/ \u6570\u636e\u5e93\u540d\n};\n\n\/\/ \u6253\u5370\u67e5\u8be2\u884c\u6570\u636e\nconst printRows = (rows) =&gt; {\n    rows.forEach(function (row) {\n        console.log(row.id, row.bookName, row.author);\n    });\n};\n\n\/\/ \u6253\u5370\u67e5\u8be2\u5b57\u6bb5\u4fe1\u606f\nconst printFields = (fields) =&gt; {\n    fields.forEach(function (field) {\n        console.log(field.name);\n    });\n};\n\ntry {\n    const connection = await mysql.createConnection(mysqlConfig);\n\n    \/\/ \u65b9\u5f0f\u4e00\uff1a\u7b80\u5355\u67e5\u8be2\n    var [results, fields] = await connection.query(\n        &#039;SELECT * FROM `test_tb`&#039;\n    );\n    printRows(results);\n    printFields(fields);\n\n    console.log(&#039;-&#039;.repeat(100));\n\n    \/\/ \u65b9\u5f0f\u4e8c\uff1a\u5360\u4f4d\u7b26\n    var [results, fields] = await connection.query(\n        &#039;SELECT * FROM `test_tb` WHERE `author` = ? AND `id` &gt; ?&#039;,\n        [&#039;author3&#039;, 1]\n    );\n    printRows(results);\n    printFields(fields);\n\n    console.log(&#039;-&#039;.repeat(100));\n\n    \/\/ \u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\n    \/\/ \u53ef\u4ee5\u9632 SQL \u6ce8\u5165\n    var [results, fields] = await connection.execute(\n        &#039;SELECT * FROM `test_tb` WHERE `author` = ? AND `id` &gt; ?&#039;,\n        [&#039;author3&#039;, 1]\n    );\n    printRows(results);\n    printFields(fields);\n\n    \/\/ \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\n    await connection.end();\n}\ncatch (error) {\n    console.error(error);\n}<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714230593435.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 384px; --smush-placeholder-aspect-ratio: 384\/390;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E5%A2%9E%E5%88%A0%E6%94%B9-2\"><\/span>\u589e\u5220\u6539<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u589e\u5220\u6539\u7684\u64cd\u4f5c\u548c\u67e5\u8be2\u7684\u64cd\u4f5c\u4e00\u6837\uff0c\u5177\u4f53\u7684\u884c\u4e3a\u4f9d\u8d56\u4e8e MySQL \u8bed\u53e5\u3002<\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E6%8F%92%E5%85%A5\"><\/span>\u63d2\u5165<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-js\">    var [results] = await connection.execute(\n        &#039;INSERT INTO test_tb (bookName, author) values (?, ?)&#039;,\n        [&#039;book4&#039;, &#039;author4&#039;]\n    );\n    console.log(&#039;Inserted &#039; + results.affectedRows + &#039; row(s).&#039;);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714231089689.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 402px; --smush-placeholder-aspect-ratio: 402\/94;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714231099307.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 381px; --smush-placeholder-aspect-ratio: 381\/158;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E4%BF%AE%E6%94%B9\"><\/span>\u4fee\u6539<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-js\">    var [results] = await connection.execute(\n        &#039;UPDATE test_tb SET bookName = ? WHERE author = ?&#039;,\n        [&#039;bookbook4&#039;, &#039;author4&#039;]\n    );\n    console.log(results.affectedRows);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714231179712.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 386px; --smush-placeholder-aspect-ratio: 386\/154;\" \/><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E5%88%A0%E9%99%A4\"><\/span>\u5220\u9664<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre><code class=\"language-js\">    var [results] = await connection.execute(\n        &#039;DELETE FROM test_tb WHERE id = ?&#039;,\n        [&#039;1&#039;]\n    );\n    console.log(results.affectedRows);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714231248652.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 367px; --smush-placeholder-aspect-ratio: 367\/127;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"sequelize_%E6%A8%A1%E5%9E%8B\"><\/span>sequelize \u6a21\u578b<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u57fa\u4e8e sequelize 6.37.3 \u5b9e\u8df5\uff0c\u5b98\u65b9\u4e2d\u6587\u6587\u6863\uff1a<a href=\"https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\">https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN<\/a><\/p>\n<pre><code class=\"language-c\">npm i sequelize@6.37.3<\/code><\/pre>\n<p>\u6309\u6211\u770b\u6587\u6863\u5b9e\u8df5\u540e\u7684\u7406\u89e3\uff0c\u6a21\u578b\u7684\u6982\u5ff5\u5c31\u662f\u628a\u8868\u6620\u5c04\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u901a\u8fc7\u5bf9\u8c61\u7684\u4e00\u4e9b\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u95f4\u63a5\u5b9e\u73b0\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\uff0c\u4e0d\u9700\u8981\u76f4\u63a5\u4f7f\u7528 SQL \u8bed\u53e5\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u57fa\u672c\u4f7f\u7528\u793a\u4f8b\uff0c\u6ce8\u91ca\u9644\u5e26\u7684\u94fe\u63a5\u662f\u5bf9\u5e94\u7684\u53c2\u8003\u90e8\u5206<\/p>\n<pre><code class=\"language-js\">import { DataTypes, Op, Sequelize } from &quot;sequelize&quot;;\n\n\/\/ \u8fde\u63a5\u6570\u636e\u5e93\n\/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/getting-started.md#%E8%BF%9E%E6%8E%A5%E5%88%B0%E6%95%B0%E6%8D%AE%E5%BA%93\n\/\/ \u6570\u636e\u5e93\u540d\uff0c\u7528\u6237\u540d\uff0c\u5bc6\u7801\uff0c\u4e3b\u673a\u5730\u5740\uff0c\u6570\u636e\u5e93\u7c7b\u578b\nconst sequelize = new Sequelize(&#039;test_db&#039;, &#039;root&#039;, &#039;1&#039;, {\n    host: &#039;localhost&#039;,\n    dialect: &#039;mysql&#039;\n});\n\n\/\/ \u8fde\u63a5\u6d4b\u8bd5\n\/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/getting-started.md#%E6%B5%8B%E8%AF%95%E8%BF%9E%E6%8E%A5\ntry {\n    await sequelize.authenticate();\n    console.log(&#039;\u6210\u529f\u5efa\u7acb\u8fde\u63a5\u3002&#039;);\n} catch (error) {\n    console.error(&#039;\u65e0\u6cd5\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1a&#039;, error);\n}\n\n\/\/ \u6a21\u578b\u5b9a\u4e49\n\/\/ \u7ed3\u5408\u6570\u636e\u5e93\u521b\u5efa\u7684\u8868\u7ed3\u6784\n\/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/model-basics.md#%E6%A8%A1%E5%9E%8B%E5%AE%9A%E4%B9%89\nclass TestTb extends Sequelize.Model {}\nTestTb.init({\n    \/\/ id \u5b57\u6bb5\u53ef\u4ee5\u4e0d\u7528\u5199\uff0c\u4f1a\u81ea\u52a8\u7ef4\u62a4\n\n    bookName: {\n        type: DataTypes.STRING(256),\n        allowNull: true\n    },\n    author: {\n        type: DataTypes.STRING(256),\n        allowNull: true\n    }\n}, {\n    sequelize,\n    modelName: &#039;TestTb&#039;,\n    \/\/ \u5982\u679c\u4e0d\u6307\u5b9a\u8868\u540d\uff0c\u5219\u4f1a\u9ed8\u8ba4\u7528\u6a21\u578b\u540d\u7684\u590d\u6570\u4f5c\u4e3a\u8868\u540d\u3002\n    \/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/model-basics.md#%E8%A1%A8%E5%90%8D%E6%8E%A8%E6%96%AD\n    tableName: &#039;test_tb&#039;,\n    \/\/ \u7981\u6b62\u65f6\u95f4\u6233\uff0c\u5426\u5219 sequelize \u4f1a\u6dfb\u52a0\u201c\u521b\u5efa\u201d\u548c\u201c\u66f4\u65b0\u201d\u4e24\u4e2a\u65f6\u95f4\u6233\u5e76\u81ea\u52a8\u7ef4\u62a4\u3002\n    \/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/model-basics.md#%E6%97%B6%E9%97%B4%E6%88%B3\n    timestamps: false\n});\n\nconst printRows = (results) =&gt; {\n    results.forEach((item) =&gt; {\n        console.log(item.dataValues.id + &#039; &#039; + item.dataValues.bookName + &#039; &#039; + item.dataValues.author);\n    });\n    console.log(&#039;-&#039;.repeat(100));\n};\n\n\/\/ \u67e5\u8be2\n\/\/ SELECT * FROM test_tb;\n\/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/model-querying-basics.md#%E7%AE%80%E5%8D%95-select-%E6%9F%A5%E8%AF%A2\nvar results = await TestTb.findAll();\nprintRows(results);\n\n\/\/ \u67e5\u8be2\u6307\u5b9a\u5b57\u6bb5\n\/\/ SELECT id, bookName FROM test_tb;\n\/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/model-querying-basics.md#select-%E6%9F%A5%E8%AF%A2%E7%89%B9%E5%AE%9A%E5%B1%9E%E6%80%A7\nvar results = await TestTb.findAll({\n    attributes: [&#039;id&#039;,&#039;bookName&#039;]\n});\nprintRows(results);\n\n\/\/ where\n\/\/ SELECT * FROM test_tb WHERE id = 2;\n\/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/model-querying-basics.md#%E5%BA%94%E7%94%A8-where-%E5%AD%90%E5%8F%A5\nvar results = await TestTb.findAll({\n    where: {\n        id : {\n            [Op.eq]: 2\n        }\n    }\n});\nprintRows(results);\n\n\/\/ \u63d2\u5165\n\/\/ INSERT INTO test_tb (bookName, author) VALUES (&#039;test&#039;, &#039;test&#039;);\n\/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/model-querying-basics.md#%E7%AE%80%E5%8D%95-insert-%E6%9F%A5%E8%AF%A2\nvar newRow = await TestTb.create({\n    bookName: &#039;test&#039;,\n    author: &#039;test&#039;\n});\nconsole.log(newRow.dataValues.id);\n\n\/\/ \u66f4\u65b0\n\/\/ UPDATE test_tb SET bookName = &#039;test2&#039; WHERE id = 3;\n\/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/model-querying-basics.md#%E7%AE%80%E5%8D%95-update-%E6%9F%A5%E8%AF%A2\nawait TestTb.update({\n    bookName: &#039;test2&#039;\n}, {\n    where: {\n        id : {\n            [Op.eq]: 3\n        }\n    }\n});\n\n\/\/ \u5220\u9664\n\/\/ DELETE FROM test_tb WHERE id = 1;\n\/\/ https:\/\/github.com\/demopark\/sequelize-docs-Zh-CN\/blob\/master\/core-concepts\/model-querying-basics.md#%E7%AE%80%E5%8D%95-delete-%E6%9F%A5%E8%AF%A2\nawait TestTb.destroy({\n    where: {\n        id : {\n            [Op.eq]: 1\n        }\n    }\n});\n\n\/\/ \u5173\u95ed\u8fde\u63a5\nsequelize.close()<\/code><\/pre>\n<h1><span class=\"ez-toc-section\" id=\"JSON\"><\/span>JSON<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>JSON \u5b98\u7f51\uff1a<a href=\"https:\/\/www.json.org\/json-zh.html\">https:\/\/www.json.org\/json-zh.html<\/a><\/p>\n<p>JSON \u7684\u57fa\u672c\u7ec4\u6210\uff1a<\/p>\n<ul>\n<li>\u5bf9\u8c61\uff1a\u7531\u5927\u62ec\u53f7\u62ec\u8d77\u6765\uff0c\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u952e\u503c\u5bf9<\/li>\n<li>\u952e\uff1a\u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\u540d\u79f0\uff0c\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\uff0c\u4e14\u4f7f\u7528\u53cc\u5f15\u53f7\u62ec\u8d77\u6765<\/li>\n<li>\u503c\uff1a\u4e0e\u952e\u5173\u8054\u7684\u5c5e\u6027\u503c\uff0c\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001\u5bf9\u8c61\u3001\u6570\u7ec4\u548c null<\/li>\n<li>\u6570\u7ec4\uff1a\u7531\u4e2d\u62ec\u53f7\u62ec\u8d77\u6765\uff0c\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u952e\u503c\u5bf9<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"%E4%BD%BF%E7%94%A8_import_%E8%AF%BB%E5%8F%96\"><\/span>\u4f7f\u7528 import \u8bfb\u53d6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5728\u6211\u4f7f\u7528\u7684\u6700\u65b0\u7a33\u5b9a\u7248 Node.js 20.12.2 \uff082024.4.29\uff09\u4e2d\uff0c\u8fd9\u8fd8\u662f\u4e00\u9879\u5b9e\u9a8c\u6027\u529f\u80fd\uff0c\u53ea\u7b80\u5355\u8bd5\u4e00\u4e0b\uff0c\u4e0d\u6253\u7b97\u6df1\u7a76\uff0c\u6bd5\u7adf\u662f\u672a\u786e\u5b9a\u7684\u529f\u80fd<\/p>\n<p>data.json<\/p>\n<pre><code class=\"language-json\">{\n  &quot;\u6c34\u679c&quot;: [\n    {\n      &quot;name&quot;: &quot;\u82f9\u679c&quot;,\n      &quot;price&quot;: 5\n    },\n    {\n      &quot;name&quot;: &quot;\u9999\u8549&quot;,\n      &quot;price&quot;: 3\n    },\n    {\n      &quot;name&quot;: &quot;\u6a59\u5b50&quot;,\n      &quot;price&quot;: 4\n    }\n  ],\n  &quot;\u852c\u83dc&quot;: [\n    {\n      &quot;name&quot;: &quot;\u80e1\u841d\u535c&quot;,\n      &quot;price&quot;: 2\n    },\n    {\n      &quot;name&quot;: &quot;\u571f\u8c46&quot;,\n      &quot;price&quot;: 1.5\n    },\n    {\n      &quot;name&quot;: &quot;\u897f\u7ea2\u67ff&quot;,\n      &quot;price&quot;: 3\n    }\n  ]\n}<\/code><\/pre>\n<p>index,js<\/p>\n<pre><code class=\"language-js\">import myJson from &quot;.\/data.json&quot; assert { type: &quot;json&quot; };\n\nconsole.log(&#039;-&#039;.repeat(100));\n\nconsole.log(myJson);\n\nconsole.log(&#039;-&#039;.repeat(100));\n\nconsole.log(myJson.\u6c34\u679c[1]);\n\nconsole.log(&#039;-&#039;.repeat(100));\n\nconsole.log(myJson.\u6c34\u679c[2].name);\n\nconsole.log(&#039;-&#039;.repeat(100));<\/code><\/pre>\n<p>\u53ef\u4ee5\u7528 <strong>.\u5bf9\u8c61<\/strong> \u6765\u83b7\u53d6\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\uff0c\u4f7f\u7528\u4e0b\u6807\u83b7\u53d6\u6570\u7ec4\u5b50\u5143\u7d20\u7684\u503c<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714374905119.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 824px; --smush-placeholder-aspect-ratio: 824\/369;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96\"><\/span>\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5e8f\u5217\u5316\u662f\u6307\u628a\u4e00\u4e2a\u7279\u5b9a\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u5bf9\u8c61\u7b49\uff09\u8f6c\u4e3a\u53ef\u4ee5\u7f51\u7edc\u4f20\u8f93\u6216\u5b58\u50a8\u7684\u683c\u5f0f\uff08\u5982\u5b57\u7b26\u4e32\u3001\u4e8c\u8fdb\u5236\u6570\u636e\u7b49\uff09\uff0c\u53cd\u5e8f\u5217\u5316\u5c31\u662f\u53cd\u8fc7\u6765<\/p>\n<p>index.js<\/p>\n<pre><code class=\"language-js\">const object = {\n  &quot;fruits&quot;: [\n    {\n      &quot;name&quot;: &quot;\u82f9\u679c&quot;,\n      &quot;price&quot;: 5\n    },\n    {\n      &quot;name&quot;: &quot;\u9999\u8549&quot;,\n      &quot;price&quot;: 3\n    },\n    {\n      &quot;name&quot;: &quot;\u6a59\u5b50&quot;,\n      &quot;price&quot;: 4\n    }\n  ],\n  &quot;vegetables&quot;: [\n    {\n      &quot;name&quot;: &quot;\u80e1\u841d\u535c&quot;,\n      &quot;price&quot;: 2\n    },\n    {\n      &quot;name&quot;: &quot;\u571f\u8c46&quot;,\n      &quot;price&quot;: 1.5\n    },\n    {\n      &quot;name&quot;: &quot;\u897f\u7ea2\u67ff&quot;,\n      &quot;price&quot;: 3\n    }\n  ]\n};\n\n\/\/ \u5e8f\u5217\u5316\n\/\/ \u5c06\u5bf9\u8c61\u8f6c\u4e3a\u5b57\u7b26\u4e32\nconst serializedObject = JSON.stringify(object);\nconsole.log(serializedObject);\n\n\/\/ \u53cd\u5e8f\u5217\u5316\n\/\/ \u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5bf9\u8c61\nconst deserializedObject = JSON.parse(serializedObject);\nconsole.log(deserializedObject);\nconsole.log(deserializedObject.fruits[1].name);<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714376145133.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1388px; --smush-placeholder-aspect-ratio: 1388\/267;\" \/><\/p>\n<h1><span class=\"ez-toc-section\" id=\"json-server\"><\/span>json-server<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>json-server \u53ef\u4ee5\u63d0\u4f9b\u63a5\u53e3\u6765\u8bbf\u95ee json \u6587\u4ef6\uff0c\u63d0\u4f9b\u7684\u63a5\u53e3\u7b26\u5408 RESTful API\u3002<br \/>\n\u53ef\u4ee5\u7528\u6765\u5b66\u4e60\u719f\u6089 RESTful API\uff0c\u4ee5\u53ca\u524d\u7aef\u5f00\u53d1\u7684\u65f6\u5019\u7528\u6765\u6a21\u62df\u540e\u7aef\u3002<br \/>\n\u57fa\u4e8e json-server 1.0.0-alpha.23 \u5b9e\u8df5\uff0c\u53c2\u8003\u6587\u6863\uff1a<a href=\"https:\/\/github.com\/typicode\/json-server\">https:\/\/github.com\/typicode\/json-server<\/a><\/p>\n<pre><code class=\"language-c\">npm i json-server@1.0.0-alpha.23<\/code><\/pre>\n<p>\u4f7f\u7528 VScode \u4e2d\u7684 Postman \u63d2\u4ef6\u6a21\u62df\u8bf7\u6c42\u6f14\u793a<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E7%94%A8%E4%BA%8E%E6%BC%94%E7%A4%BA%E7%9A%84_json\"><\/span>\u7528\u4e8e\u6f14\u793a\u7684 json<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>data.json<\/p>\n<pre><code class=\"language-json\">{\n    &quot;posts&quot;: [\n        {\n            &quot;id&quot;: &quot;1&quot;,\n            &quot;title&quot;: &quot;a title&quot;,\n            &quot;views&quot;: 100\n        },\n        {\n            &quot;id&quot;: &quot;2&quot;,\n            &quot;title&quot;: &quot;another title&quot;,\n            &quot;views&quot;: 200\n        }\n    ],\n    &quot;comments&quot;: [\n        {\n            &quot;id&quot;: &quot;1&quot;,\n            &quot;text&quot;: &quot;a comment about post 1&quot;,\n            &quot;postId&quot;: &quot;1&quot;\n        },\n        {\n            &quot;id&quot;: &quot;2&quot;,\n            &quot;text&quot;: &quot;another comment about post 1&quot;,\n            &quot;postId&quot;: &quot;1&quot;\n        }\n    ],\n    &quot;profile&quot;: {\n        &quot;name&quot;: &quot;typicode&quot;\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c json-server<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714323419707.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 588px; --smush-placeholder-aspect-ratio: 588\/324;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"GET\"><\/span>GET<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>GET \u65b9\u6cd5\u7528\u4e8e\u8bf7\u6c42\u6570\u636e<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E9%A1%B6%E5%B1%82%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7\"><\/span>\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u53d1\u8d77 GET \u8bf7\u6c42\u540e\u8ddf\u4e0a\u8def\u5f84\uff0c\u8def\u5f84\u4e3a json \u4e2d\u7684\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\uff0c\u5373\u53ef\u83b7\u53d6\u5bf9\u5e94\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\u7684\u503c<\/p>\n<pre><code class=\"language-c\">GET \/\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714323501848.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 624px; --smush-placeholder-aspect-ratio: 624\/587;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714323528253.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 483px; --smush-placeholder-aspect-ratio: 483\/534;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714323594763.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 360px; --smush-placeholder-aspect-ratio: 360\/389;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E9%A1%B6%E5%B1%82%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E7%9A%84%E5%80%BC%E4%B8%BA%E6%95%B0%E7%BB%84%E6%97%B6%E8%AE%BF%E9%97%AE%E5%AD%90%E5%85%83%E7%B4%A0\"><\/span>\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\u7684\u503c\u4e3a\u6570\u7ec4\u65f6\u8bbf\u95ee\u5b50\u5143\u7d20<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>posts \u548c comments \u503c\u90fd\u662f\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5b50\u5143\u7d20\u5b58\u5728\u4e00\u4e2a id\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a id \u8fdb\u4e00\u6b65\u83b7\u53d6\u6240\u5728\u5b50\u5143\u7d20<\/p>\n<pre><code class=\"language-c\">GET \/\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\/\u5b50\u5143\u7d20id<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714323927319.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 333px; --smush-placeholder-aspect-ratio: 333\/348;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714323939612.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 345px; --smush-placeholder-aspect-ratio: 345\/351;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714323957175.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 389px; --smush-placeholder-aspect-ratio: 389\/344;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714323966672.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 448px; --smush-placeholder-aspect-ratio: 448\/351;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"POST\"><\/span>POST<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>POST \u65b9\u6cd5\u7528\u4e8e\u5411\u670d\u52a1\u5668\u6dfb\u52a0\u6570\u636e<\/p>\n<pre><code class=\"language-c\">POST \/\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\/\u5b50\u5143\u7d20id<\/code><\/pre>\n<p>\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\u503c\u4e3a\u6570\u7ec4\u7684\uff0c\u53ef\u4ee5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u6570\u636e<\/p>\n<p>\u6dfb\u52a0\u7684\u65f6\u5019\u9700\u8981\u4e9b id \u5b57\u6bb5\uff0cjson-server \u4f1a\u81ea\u52a8\u6dfb\u52a0\u552f\u4e00\u6807\u8bc6<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714324371593.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 699px; --smush-placeholder-aspect-ratio: 699\/596;\" \/><br \/>\n\u67e5\u770b json \u6587\u4ef6\u53ef\u4ee5\u770b\u5230\u6dfb\u52a0\u7684\u6570\u636e<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714324389405.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 489px; --smush-placeholder-aspect-ratio: 489\/416;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"PUT\"><\/span>PUT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>PUT \u65b9\u6cd5\u7528\u4e8e\u4fee\u6539\u6570\u636e\uff0c\u4fee\u6539\u7684\u90e8\u5206\u4e0d\u7ba1\u539f\u6765\u5185\u5bb9\u662f\u4ec0\u4e48\uff0c\u76f4\u63a5\u7528\u63d0\u4ea4\u7684\u5185\u5bb9\u8986\u76d6\u4e0a\u53bb<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E4%BF%AE%E6%94%B9%E6%95%B0%E7%BB%84%E5%AD%90%E5%85%83%E7%B4%A0\"><\/span>\u4fee\u6539\u6570\u7ec4\u5b50\u5143\u7d20<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-c\">PUT \/\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\/\u5b50\u5143\u7d20id<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714324615709.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 669px; --smush-placeholder-aspect-ratio: 669\/632;\" \/><\/p>\n<p>\u4fee\u6539\u540e\u7684 json \u6587\u4ef6\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u63d0\u4ea4\u7684\u5185\u5bb9\u76f4\u63a5\u8986\u76d6\u4e86\u539f ID = 1 \u7684\u5185\u5bb9<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714324734786.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 330px; --smush-placeholder-aspect-ratio: 330\/200;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E4%BF%AE%E6%94%B9%E9%A1%B6%E5%B1%82%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E5%80%BC\"><\/span>\u4fee\u6539\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\u503c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-c\">PUT \/\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714324798746.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 656px; --smush-placeholder-aspect-ratio: 656\/600;\" \/><br \/>\n\u53ef\u4ee5\u770b\u5230\u4e5f\u662f\u8986\u76d6\u4e86\u539f\u6765\u7684\u5185\u5bb9<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714324812819.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 254px; --smush-placeholder-aspect-ratio: 254\/142;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"PATCH\"><\/span>PATCH<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>PATCH \u7528\u4e8e\u4fee\u6539\u6570\u636e\uff0c\u4fee\u6539\u65b9\u5f0f\u548c PUT \u4e0d\u540c\uff0c\u5982\u679c\u63d0\u4ea4\u7684\u5b57\u6bb5\u5b58\u5728\u5219\u4fee\u6539\u4e3a\u63d0\u4ea4\u7684\uff0c\u4e0d\u5b58\u5728\u5219\u65b0\u589e\u5b57\u6bb5<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E4%BF%AE%E6%94%B9%E6%95%B0%E7%BB%84%E5%AD%90%E5%85%83%E7%B4%A0-2\"><\/span>\u4fee\u6539\u6570\u7ec4\u5b50\u5143\u7d20<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-c\">PATCH \/\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\/\u5b50\u5143\u7d20id<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714324983132.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 675px; --smush-placeholder-aspect-ratio: 675\/624;\" \/><br \/>\n\u63d0\u4ea4\u7684 text \u4fee\u6539\u4e86\u539f text \u5185\u5bb9\uff0c\u800c age \u662f\u539f\u6765\u6ca1\u6709\u7684\uff0c\u5219\u4f1a\u65b0\u589e\u4e0a\u53bb<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714324992627.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 419px; --smush-placeholder-aspect-ratio: 419\/245;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E4%BF%AE%E6%94%B9%E9%A1%B6%E5%B1%82%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E5%80%BC%EF%BC%88%E6%9C%89_bug%EF%BC%9F%EF%BC%89\"><\/span>\u4fee\u6539\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\u503c\uff08\u6709 bug\uff1f\uff09<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-c\">PATCH \/\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714325453547.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 670px; --smush-placeholder-aspect-ratio: 670\/625;\" \/><\/p>\n<p>age \u660e\u660e\u6709\u4e00\u6837\u7684\u5b57\u6bb5\uff0c\u7ed3\u679c\u53d8\u6210\u4e86\u65b0\u589e\uff0c\u4f3c\u4e4e json-server \u6709 bug<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714325481609.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 264px; --smush-placeholder-aspect-ratio: 264\/232;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"DELETE\"><\/span>DELETE<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u7528\u4e8e\u5220\u9664\u6570\u636e<\/p>\n<pre><code class=\"language-c\">DELETE \/\u9876\u5c42\u5bf9\u8c61\u5c5e\u6027\/\u5b50\u5143\u7d20id<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714325596776.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 606px; --smush-placeholder-aspect-ratio: 606\/578;\" \/><br \/>\n\u5220\u9664\u4e86 ID = 1 \u7684\u5b50\u5143\u7d20<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714325612332.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 438px; --smush-placeholder-aspect-ratio: 438\/290;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%8F%82%E6%95%B0%E6%94%AF%E6%8C%81\"><\/span>\u53c2\u6570\u652f\u6301<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\"https:\/\/github.com\/typicode\/json-server?tab=readme-ov-file#params\">https:\/\/github.com\/typicode\/json-server?tab=readme-ov-file#params<\/a><\/p>\n<h1><span class=\"ez-toc-section\" id=\"Promise\"><\/span>Promise<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u5728\u5b66 Node.js \u7684\u65f6\u5019\uff0c\u6211\u662f\u8ddf\u7740\u5c1a\u7845\u8c37\u7684\u4e00\u4e2a\u516c\u5f00\u8bfe\u7a0b\u5b9e\u8df5\u7684\uff0c\u90a3\u4e2a\u8bfe\u7a0b\u8fd8\u662f\u7528\u7684 CJS\uff0c\u7136\u540e\u6211\u7ed3\u5408\u8bfe\u7a0b\u8bb2\u7684\u548c Node.js \u7684\u5b98\u65b9\u6587\u6863\u8fdb\u884c ESM \u7684\u5b9e\u8df5\u9a8c\u8bc1\u3002\u76f4\u5230\u540e\u9762\u9879\u76ee\u5b9e\u8df5\u90e8\u5206\uff0c\u8bfe\u7a0b\u91cc\u4f7f\u7528\u7684\u6570\u636e\u5e93\u662f MongoDB\uff0c\u6211\u60f3\u7528 MySQL\u3002\u5c31\u53e6\u5916\u627e\u4e86\u4e2a\u7528 MySQL \u7684\u9879\u76ee\u5b9e\u8df5\u89c6\u9891\u770b\u770b\u7528\u4ec0\u4e48\u9a71\u52a8\uff0c\u4e5f\u5c31\u662f\u4e0a\u6587\u4e2d\u7684 mysql \u9a71\u52a8\uff0c\u6211\u8ddf\u7740\u6587\u6863\u5b9e\u8df5\u4e86\u4e00\u4e0b\uff0c\u540e\u9762\u5728\u770b\u53d1\u5e03\u7248\u672c\u7684\u65f6\u5019\u6ce8\u610f\u5230\u5df2\u7ecf\u505c\u66f4 4 \u5e74\u591a\u4e86\uff0c\u53c8\u53bb\u627e\u522b\u7684\u9a71\u52a8\uff0c\u4e8e\u662f\u53c8\u53d1\u73b0\u4e86\u4e0a\u6587\u4e2d\u7684 mysql2\uff0c\u518d\u8ddf\u7740\u6587\u6863\u5b9e\u8df5\u64cd\u4f5c\uff0c\u624d\u53d1\u73b0\u6709\u4e2a Promise \u7248\u672c\u7684\u7528\u6cd5\uff0c\u624d\u77e5\u9053 Promise \u7684\u5b58\u5728\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E7%A4%BA%E4%BE%8B_1\"><\/span>\u793a\u4f8b 1<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-js\">\/\/ \u65b0\u5efa\u4e00\u4e2a Promise \u5bf9\u8c61\nconst fun1 = new Promise((resolve, reject) =&gt; {\n    const condition = false; \/\/ \u5047\u8bbe\u8fd9\u662f\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c\n    if (condition) {\n        resolve(&quot;\u6210\u529f&quot;);\n    }\n    else {\n        reject(&quot;\u5931\u8d25&quot;);\n    }\n});\n\nfun1\n    .then((result) =&gt; { \/\/ then \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5728 resolve \u88ab\u8c03\u7528\u65f6\u6267\u884c\n        console.log(result);\n    })\n    .catch((error) =&gt; { \/\/ catch \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5728 reject \u88ab\u8c03\u7528\u65f6\u6267\u884c\n        console.log(error);\n    });<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714389800258.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 434px; --smush-placeholder-aspect-ratio: 434\/106;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E7%A4%BA%E4%BE%8B_2\"><\/span>\u793a\u4f8b 2<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-js\">\/\/ \u4f20\u5165\u53c2\u6570\u662f\u5076\u6570\u5219\u9664\u4ee5 2\uff0c\u5426\u5219\u629b\u51fa\u9519\u8bef\nfunction fun1(param) {\n    const p = new Promise((resolve, reject) =&gt; {\n        const condition = param % 2 === 0; \/\/ \u8ba1\u7b97\u662f\u5426\u6ee1\u8db3\u5076\u6570\u6761\u4ef6\n        if (condition) {\n            const result = param \/ 2;\n            resolve(result);\n        }\n        else {\n            reject(`\u9519\u8bef\uff0c\u53c2\u6570 ${param} \u4e0d\u662f\u5947\u6570`);\n        }\n    })\n    return p;\n}\n\n\/\/ Promise \u662f\u5f02\u6b65\u6267\u884c\u7684\uff0c\u5982\u679c\u8981\u7528 try \u6355\u83b7\u9700\u8981\u52a0\u4e0a await\n\/\/ \u8fd9\u6837\u4e3b\u7ebf\u7a0b\u4f1a\u7b49\u5f85 Promise \u6267\u884c\u7ed3\u675f\n\n\/\/ \u4e0d\u6ee1\u8db3\u6761\u4ef6\u629b\u51fa\u9519\u8bef\ntry {\n    await fun1(5);\n}\ncatch (error) {\n    console.log(error);\n}\n\n\/\/ \u6ee1\u8db3\u6761\u4ef6\u8f93\u51fa 2\ntry {\n    await fun1(4).then((result) =&gt; {\n        console.log(result);\n    });\n}\ncatch (error) {\n    console.log(error);\n}\n\n\/\/ \u6ee1\u8db3\u6761\u4ef6\n\/\/ \u8f93\u51fa 1\nfun1(2)\n.then((result) =&gt; {\n    console.log(result);\n})\n.catch((error) =&gt; {\n    console.log(error);\n});\n\n\/\/ \u76f8\u5bf9\u56de\u8c03\u51fd\u6570\u7684\u4f18\u52bf\u5c31\u4f53\u73b0\u5728\u8fd9\u91cc\n\/\/ \u8981\u662f\u7528\u56de\u8c03\u51fd\u6570\u5c31\u4f1a\u5927\u62ec\u53f7\u4e00\u5c42\u5957\u4e00\u5c42\uff0c\u51fd\u6570\u5d4c\u5957\u8c03\u7528\uff0c\u4ee3\u7801\u4e00\u76f4\u5f80\u53f3\u5ef6\u4f38\uff0c\u79f0\u4e3a\u201c\u56de\u8c03\u5730\u72f1\u201d\n\/\/ \u7528 Promise \u53ea\u662f\u5f80\u4e0b\u5ef6\u4f38\nfun1(4)\n.then((result) =&gt; {\n    console.log(result); \/\/ \u8f93\u51fa 2\n    return fun1(result); \/\/ \u5c06 2 \u4ee3\u5165 Promise \u518d\u6b21\u5904\u7406\n})\n.then((result) =&gt; {\n    console.log(result); \/\/ \u6253\u5370\u4e0a\u4e00\u4e2a Promise \u5904\u7406\u7684\u7ed3\u679c 1\n    return fun1(result); \/\/ \u5c06 1 \u4ee3\u5165 Promise \u518d\u6b21\u5904\u7406\uff0c1 \u4e3a\u5947\u6570\u4f1a\u89e6\u53d1\u5f02\u5e38\n})\n.catch((error) =&gt; {\n    console.log(error);\n});<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714393346326.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 410px; --smush-placeholder-aspect-ratio: 410\/198;\" \/><\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E4%BC%9A%E8%AF%9D%E6%8E%A7%E5%88%B6\"><\/span>\u4f1a\u8bdd\u63a7\u5236<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u5f53\u6709\u591a\u4e2a\u7528\u6237\u8bbf\u95ee\u670d\u52a1\u7684\u65f6\u5019\uff0c\u670d\u52a1\u5668\u65e0\u6cd5\u533a\u5206\u7528\u6237\uff0c\u9700\u8981\u901a\u8fc7\u7279\u5b9a\u7684\u65b9\u6cd5\u6765\u533a\u522b\uff0c\u76f4\u63a5\u4f53\u73b0\u5728\u767b\u5f55\u8d26\u53f7\uff0c\u6280\u672f\u5c42\u9762\u5b9e\u73b0\u6709\u4e0b\u9762\u65b9\u6cd5\uff1a<\/p>\n<h2><span class=\"ez-toc-section\" id=\"cookie\"><\/span>cookie<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Cookie \u662f\u4e00\u79cd\u5728\u5ba2\u6237\u7aef\u5b58\u50a8\u6570\u636e\u7684\u673a\u5236<br \/>\n\u4e00\u4e2a\u7f51\u7ad9\u901a\u8fc7\u8d26\u53f7\u533a\u5206\u7528\u6237\uff0c\u5176\u4f5c\u7528\u539f\u7406\uff1a\u6d4f\u89c8\u5668\u5728\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u5982\u679c\u672c\u5730\u5b58\u50a8\u6709 cookie\uff0c\u5c31\u4f1a\u628a cookie \u4e00\u8d77\u53d1\u7ed9\u670d\u52a1\u5668\u3002\u521d\u59cb\u72b6\u6001\uff0c\u7528\u6237\u6ca1\u6709\u7279\u5b9a\u7684 cookie \u5185\u5bb9\uff0c\u5728\u8bbf\u95ee\u7f51\u7ad9\u7684\u65f6\u5019\uff0c\u670d\u52a1\u5668\u53ef\u4ee5\u8bc6\u522b\u51fa\u7528\u6237\u662f\u672a\u767b\u5f55\u72b6\u6001\u3002\u5982\u679c\u7528\u6237\u53d1\u8d77\u767b\u5f55\uff0c\u8d26\u53f7\u5bc6\u7801\u5728\u670d\u52a1\u5668\u9a8c\u8bc1\u901a\u8fc7\u540e\uff0c\u5c31\u53d1\u56de\u4e00\u4e9b cookie \u5b57\u6bb5\uff0c\u8fd9\uff08\u4e9b\uff09\u5b57\u6bb5\u662f\u548c\u8d26\u53f7\u662f\u5173\u8054\u7684\uff0c\u540e\u7eed\u7528\u6237\u53d1\u8d77\u8bf7\u6c42\u90fd\u9700\u8981\u643a\u5e26\u8fd9\u4e2a cookie\uff0c\u670d\u52a1\u5668\u5c31\u77e5\u9053\u5f53\u524d\u8bf7\u6c42\u662f\u54ea\u4e2a\u8d26\u53f7\u53d1\u51fa\u7684\u3002<\/p>\n<pre><code class=\"language-js\">import cookieParser from &#039;cookie-parser&#039;;\nimport express from &#039;express&#039;;\n\nconst app = express();\n\napp.use(cookieParser()); \/\/ \u9700\u8981\u5b89\u88c5 cookie-parser \u5305\n\n\/\/ \u8bbf\u95ee\u9875\u9762\napp.get(&#039;\/&#039;, (req, res) =&gt; {\n    res.send({\n        msg: &quot;Hello World!&quot;,\n        cookies: req.cookies \/\/ \u670d\u52a1\u5668\u6536\u5230\u7684 cookie \u5f71\u54cd\u7ed9\u5ba2\u6237\u7aef\n    });\n});\n\n\/\/ \u8bbe\u7f6e cookie\napp.get(&#039;\/set-cookie&#039;, (req, res) =&gt; {\n    res.cookie(\n        &#039;example-cookie&#039;, \/\/ cookie \u540d\n        &#039;1234567890abcdefgh&#039;, \/\/ cookie \u503c\n        {\n            maxAge: 1000 * 60 \/\/ cookie \u6709\u6548\u671f\uff08\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\uff09\uff0c\u4e0d\u5199\u7684\u8bdd\uff0c\u9ed8\u8ba4\u662f\u5173\u95ed\u6d4f\u89c8\u5668\u540e\u5931\u6548\n        }\n    );\n    res.send(&#039;Cookie set&#039;);\n});\n\n\/\/ \u5220\u9664 cookie\napp.get(&#039;\/remove-cookie&#039;, (req, res) =&gt; {\n    res.clearCookie(&#039;example-cookie&#039;);\n    res.send(&#039;Cookie removed&#039;);\n});\n\napp.listen(3000, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:3000&#039;);\n})<\/code><\/pre>\n<p>\u5728\u672a\u7533\u8bf7 cookie \u7684\u65f6\u5019\u7684\u8bbf\u95ee\uff0ccookie \u4e2d\u6709\u4e00\u4e2a\u533f\u540d ID\uff0c\u53ef\u80fd\u662f\u6d4f\u89c8\u5668\u81ea\u52a8\u751f\u6210\u7684<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714452389306.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1225px; --smush-placeholder-aspect-ratio: 1225\/749;\" \/><\/p>\n<p>\u7533\u8bf7 cookie\uff0c\u53ef\u4ee5\u770b\u5230\u54cd\u5e94\u6807\u5934\u4e2d\u6709 Set-Cookie \u5b57\u6bb5<br \/>\n\u6d4f\u89c8\u5668\u6536\u5230\u8fd9\u4e2a\u5b57\u6bb5\u5c31\u4f1a\u4fdd\u5b58 cookie\uff0c\u5176\u4e2d\u5305\u542b\u4e86 cookie \u5185\u5bb9\u548c\u6709\u6548\u65f6\u95f4<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714451004064.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1301px; --smush-placeholder-aspect-ratio: 1301\/739;\" \/><\/p>\n<p>\u518d\u6b21\u8bbf\u95ee\uff0c\u53ef\u4ee5\u770b\u5230\u8bf7\u6c42\u6807\u5934\u643a\u5e26\u4e0a\u4e86\u670d\u52a1\u5668\u8bbe\u7f6e\u7684 Cookie<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714452412789.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1281px; --smush-placeholder-aspect-ratio: 1281\/802;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"session_id\"><\/span>session id<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4f1a\u8bdd ID\uff0c\u662f\u4f1a\u8bdd\u7684\u552f\u4e00\u6807\u8bc6\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4f1a\u6a21\u62df\u4e00\u4e2a\u767b\u5f55\uff0c\u5728\u8bbf\u95ee\u7f51\u9875\u7684\u65f6\u5019\u4f1a\u643a\u5e26 cookie \u53bb\u8bf7\u6c42\u6570\u636e\u3002\u5728\u6ca1\u6709\u767b\u5f55\u7684\u60c5\u51b5\u4e0b\uff0ccookie \u4e2d\u6ca1\u6709\u6709\u6548\u7684\u4f1a\u8bdd ID\uff0c\u767b\u5f55\u9a8c\u8bc1\u4e4b\u540e\uff0c\u670d\u52a1\u5668\u4f1a\u751f\u6210\u4f1a\u8bdd ID \u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u540c\u65f6\u5728\u54cd\u5e94\u6807\u5934\u4e2d\u5c06\u4f1a\u8bdd ID \u53d1\u7ed9\u5ba2\u6237\u7aef\u4fdd\u5b58\u5230 cookie \u4e2d\uff0c\u540e\u7eed\u8bbf\u95ee\u65f6\uff0c\u4f1a\u8bdd ID \u4f34\u968f cookie \u4e00\u8d77\u53d1\u7ed9\u670d\u52a1\u5668\uff0c\u670d\u52a1\u5668\u4f1a\u68c0\u67e5\u4f1a\u8bdd ID \u662f\u5426\u5b58\u5728\u4e8e\u6570\u636e\u5e93\u4e2d\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4fdd\u6301\u4e00\u4e2a\u767b\u5f55\u72b6\u6001\u3002<\/p>\n<p>\u8fd9\u91cc\u5b9e\u8df5\u91c7\u7528 MySQL \u6570\u636e\u5e93\u6765\u5b58\u50a8\u4f1a\u8bdd ID\u3002\u9700\u8981\u4f7f\u7528\u5230\u524d\u9762\u7528\u5230\u7684 mysql2 \u9a71\u52a8\u548c Sequelize \u5b9e\u73b0\u5bf9\u8c61\u5230\u6570\u636e\u5e93\u7684\u6620\u5c04\u3002\u53e6\u5916\u989d\u5916\u9700\u8981\u4f7f\u7528 express-session \u6765\u5b9e\u73b0\u4f1a\u8bdd ID \u7684\u5904\u7406\uff0c\u4ee5\u53ca\u4f7f\u7528 connect-session-sequelize \u5b9e\u73b0 express-session \u548c Sequelize \u7684\u5bf9\u63a5\uff0c\u4ee5\u505a\u5230\u4f1a\u8bdd ID \u81ea\u52a8\u751f\u6210\u548c\u7ef4\u62a4\u3002<\/p>\n<p>express-session \u6587\u6863\uff1a<a href=\"https:\/\/github.com\/expressjs\/session\">https:\/\/github.com\/expressjs\/session<\/a><br \/>\nconnect-session-sequelize \u6587\u6863\uff1a<a href=\"https:\/\/github.com\/mweibel\/connect-session-sequelize\">https:\/\/github.com\/mweibel\/connect-session-sequelize<\/a><\/p>\n<pre><code class=\"language-js\">import express from &#039;express&#039;;\nimport session from &#039;express-session&#039;;\nimport { Sequelize } from &#039;sequelize&#039;;\n\nimport SequelizeStore from &#039;connect-session-sequelize&#039;;\nconst SequelizeStoreObject = SequelizeStore(session.Store);\n\nconst app = express();\n\n\/\/ \u6620\u5c04\u5230\u6570\u636e\u5e93\nconst sequelize = new Sequelize(\n    &#039;test_db&#039;, \/\/ \u6570\u636e\u5e93\u540d\n    &#039;root&#039;, \/\/ \u7528\u6237\u540d\n    &#039;1&#039;, \/\/ \u5bc6\u7801\n    {\n        host: &#039;localhost&#039;, \/\/ \u6570\u636e\u5e93\u5730\u5740\n        dialect: &#039;mysql&#039;, \/\/ \u6570\u636e\u5e93\u7c7b\u578b\n        timezone: &#039;+08:00&#039; \/\/ \u65f6\u533a\n    });\n\n\/\/ \u6d4b\u8bd5\u6570\u636e\u5e93\u8fde\u63a5\ntry {\n    await sequelize.authenticate();\n    console.log(&#039;\u6210\u529f\u5efa\u7acb\u8fde\u63a5\u3002&#039;);\n} catch (error) {\n    console.error(&#039;\u65e0\u6cd5\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1a&#039;, error);\n}\n\n\/\/ \u521b\u5efa\u4f1a\u8bdd ID \u5b58\u50a8\u5b9e\u4f8b\nconst myStore = new SequelizeStoreObject({\n    db: sequelize,\n});\n\napp.use(session({\n    name: &#039;sid&#039;, \/\/ \u901a\u8fc7 cookie \u8bbe\u7f6e\u4f1a\u8bdd ID\n    secret: &#039;123456&#039;, \/\/ \u7528\u4e8e\u7b7e\u540d\u7684\u5bc6\u94a5\uff0c\u9632\u4f1a\u8bdd\u7be1\u6539\n    saveUninitialized: false, \/\/ \u662f\u5426\u4fdd\u5b58\u672a\u521d\u59cb\u5316\u7684\u4f1a\u8bdd\uff0c\u9ed8\u8ba4\u4e3a true\u3002\n    store: myStore,\n    resave: false, \/\/ \u662f\u5426\u6bcf\u6b21\u8bf7\u6c42\u90fd\u91cd\u65b0\u4fdd\u5b58\u4f1a\u8bdd\uff0c\u9ed8\u8ba4\u4e3a true\u3002\n    rolling: true, \/\/ \u662f\u5426\u5728\u6bcf\u6b21\u8bf7\u6c42\u65f6\u5237\u65b0\u4f1a\u8bdd\u8fc7\u671f\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3a false\u3002\u8bbe\u4e3a true\uff0c\u4e00\u76f4\u5728\u6d3b\u52a8\u5c31\u4f1a\u5ef6\u957f\u6709\u6548\u671f\uff0c\u800c\u4e0d\u662f\u6309\u7167\u8bbe\u7f6e cookie \u7684\u65f6\u95f4\u8ba1\u7b97 cookie \u6709\u6548\u671f\n    cookie: {\n        maxAge: 1000 * 5, \/\/ \u6709\u6548\u671f\uff08\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\uff09\n        httpOnly: true, \/\/ \u662f\u5426\u53ea\u5141\u8bb8 HTTP \u8bbf\u95ee\uff08\u7981\u6b62\u5ba2\u6237\u7aef js \u83b7\u53d6\uff09\n\n    }\n}));\n\nmyStore.sync(); \/\/ \u540c\u6b65\u4f1a\u8bdd\u5b58\u50a8\u8868\uff08\u4e0d\u5b58\u5728\u8868\u4f1a\u81ea\u52a8\u521b\u5efa\uff09\n\n\/\/ \u8bbf\u95ee\u9875\u9762\napp.get(&#039;\/&#039;, (req, res) =&gt; {\n    if (req.session.isLoggedIn) {\n        res.send(`\u6b22\u8fce\u56de\u6765\uff0c${req.session.user}`);\n    }\n    else {\n        res.send(&#039;\u8bf7\u5148\u767b\u5f55&#039;);\n    }\n});\n\n\/\/ \u767b\u5f55\napp.get(&#039;\/login&#039;, (req, res) =&gt; {\n    if (req.query.user === &#039;admin&#039; &amp;&amp; req.query.password === &#039;admin&#039;) {\n        req.session.isLoggedIn = true;\n        req.session.user = &#039;IYATT-yx&#039;;\n        res.send(&#039;\u767b\u5f55\u6210\u529f&#039;);\n    }\n    else {\n        res.send(&#039;\u767b\u5f55\u5931\u8d25&#039;);\n    }\n});\n\n\/\/ \u767b\u51fa\napp.get(&#039;\/logout&#039;, (req, res) =&gt; {\n    req.session.destroy(() =&gt; { \/\/ \u4f1a\u5220\u9664\u6570\u636e\u5e93\u4e2d\u7684\u4f1a\u8bdd ID\n        res.send(&#039;\u6ce8\u9500\u6210\u529f&#039;);\n    })\n});\n\napp.listen(3000, () =&gt; {\n    console.log(&#039;Server is running on http:\/\/localhost:3000&#039;);\n})<\/code><\/pre>\n<p>\u672a\u767b\u5f55\u72b6\u6001<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714479744903.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 381px; --smush-placeholder-aspect-ratio: 381\/179;\" \/><\/p>\n<p>\u767b\u5f55<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714479762731.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 624px; --smush-placeholder-aspect-ratio: 624\/180;\" \/><\/p>\n<p>\u518d\u6b21\u8bbf\u95ee<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714479787379.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 399px; --smush-placeholder-aspect-ratio: 399\/180;\" \/><\/p>\n<p>\u9000\u51fa\u767b\u5f55<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/04\/image-1714479807844.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 868px; --smush-placeholder-aspect-ratio: 868\/178;\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"token\"><\/span>token<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5c06\u7528\u6237\u4fe1\u606f\u91c7\u7528\u4e00\u5b9a\u7684\u65b9\u5f0f\u52a0\u5bc6\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u540e\u7eed\u7528\u6237\u8bf7\u6c42\u643a\u5e26\u8fd9\u4e2a\u5b57\u7b26\u4e32\uff0c\u670d\u52a1\u5668\u53ef\u4ee5\u89e3\u5bc6\u5b57\u7b26\u4e32\u8fd8\u539f\u5176\u4e2d\u5b58\u50a8\u7684\u4fe1\u606f\uff08\u5982\u679c\u5728\u6709\u6548\u671f\u5185\uff09<\/p>\n<p>\u4f7f\u7528 jsonwebtoken \u8fdb\u884c\u5b9e\u8df5\uff0c\u6587\u6863\uff1a<a href=\"https:\/\/github.com\/auth0\/node-jsonwebtoken\">https:\/\/github.com\/auth0\/node-jsonwebtoken<\/a><\/p>\n<pre><code class=\"language-bash\">npm i jsonwebtoken@9.0.2<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6a21\u5757\u662f CJS \u7684\uff0c\u8fd9\u91cc\u7528 ESM \u65b9\u5f0f\u5bfc\u5165\u5f97\u7528\u5176\u5b83\u65b9\u6cd5\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8\"><\/span>\u7b80\u5355\u4f7f\u7528<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-js\">import jwt from &#039;jsonwebtoken&#039;;\nconst { sign, verify } = jwt;\n\n\/\/ \u4fe1\u606f\nconst info = {\n    user: &#039;IYATT-yx&#039;,\n    pass: &#039;123456&#039;\n}\n\n\/\/ \u7b7e\u540d\nconst key = &#039;123456&#039;\n\nconst token = sign(\n    info,\n    key, {\n        expiresIn: 60, \/\/ \u8fc7\u671f\u65f6\u95f4\n    }\n)\n\n\/\/ \u751f\u6210\u7684 token\nconsole.log(&#039;token: &#039;, token)\n\n\/\/ \u9a8c\u8bc1\u6210\u529f\uff08\u6ca1\u6709\u8fc7\u671f\uff0c\u6ca1\u6709\u7be1\u6539\uff09\u6253\u5370 token \u4e2d\u643a\u5e26\u7684\u4fe1\u606f\nconst result = verify(token, key, (err, data) =&gt; {\n    if (err) {\n        console.log(&#039;\u9a8c\u8bc1\u5931\u8d25&#039;)\n    } else {\n        console.log(&#039;\u9a8c\u8bc1\u6210\u529f&#039;)\n        console.log(data)\n    }\n})<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/05\/image-1714724648398.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 989px; --smush-placeholder-aspect-ratio: 989\/173;\" \/><\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E9%A1%B9%E7%9B%AE%E5%AE%9E%E8%B7%B5\"><\/span>\u9879\u76ee\u5b9e\u8df5<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ul>\n<li><a href=\"https:\/\/github.com\/IYATT-yx\/Accounts\">https:\/\/github.com\/IYATT-yx\/Accounts<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u524d\u9762\u521a\u521a\u5bf9 Spring Boot \u6709\u4e86\u4e2a\u6982\u5ff5\uff0c\u518d\u6765\u5b66\u5b66 Node.js\uff0c\u987a\u4fbf\u5f53\u5b66 JavaScript [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,577],"tags":[],"class_list":["post-14717","post","type-post","status-publish","format-standard","hentry","category-all","category-web"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/14717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14717"}],"version-history":[{"count":0,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/14717\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}