# NodeJs 项目 **Repository Path**: hyo-ja/node-js-project ## Basic Information - **Project Name**: NodeJs 项目 - **Description**: Project includes: Node.js - **Primary Language**: NodeJS - **License**: Not specified - **Default Branch**: master - **Homepage**: https://monkeymeerkat.cn/ - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-02-18 - **Last Updated**: 2025-03-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: Nodejs ## README ### 一、Nodejs 项目部署 #### **1. 项目配置**: - **初始化项目** `npm init` - **安装依赖项** `npm i` #### **2. 初始化项目:** - **手动初始化项目** **package.json** ```json { "name": "demo", "main": "app.js", "author": { "name": "小明", "email": "414141@abc.com" }, "scripts": { "rr": "node app.js" }, "dependencies": { "axios": "latest" }, "type": "module" } ``` ![image-20250218134018099](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202502181340128.png) - **使用 npm 初始化项目** ![image-20250218133226881](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202502181414012.png) ![image-20250218133416571](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202502181334623.png) #### **3. 托管项目:** 1. **.gitignore(忽略文件 )** ```bash node_modules ``` 2. **上传码云仓库** 1. **创建 git 仓库** `git init` ![image-20250218135432605](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202502181354629.png) 2. **添加提交文件** `git add .` ![image-20250218135603628](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202502181414549.png) 3. **提交信息** `git commit -m "第一次提交"` ![image-20250218140029795](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202502181414043.png) 4. **连接远程仓库** `git remote add origin git@gitee.com:hyo-ja/node-js-project.git` ![image-20250218140236383](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202502181402402.png) 5. **推送到远程仓库** `git push -u origin master` ![image-20250218141126754](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202502181411783.png) ### 二、模块 #### **1. CommonJS(同步)** 1. **特点:** - CommonJS 最初设计用于服务器端(Node.js)。 - 它的模块加载是同步的,但在浏览器中性能不佳,因为浏览器需要异步加载脚本。 2. **使用方式:** **main.js** ```js // CommonJS // 创建函数 function f1() { console.log("我是方法1"); } function f2() { console.log("我是方法2"); } function f3() { console.log("我是方法3"); } // 导出函数方法 module.exports = { f1, f2, f3 }; ``` **app.js** ```js // CommonJS // 把拿到的模块对象导入给变量 let obj = require("./Utils/main"); // 执行对象里面方法 obj.f1(); obj.f2(); obj.f3(); ``` ![image-20250218151811339](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202502181518364.png) #### **2. ES 模块(异步)** 1. **特点:** - 支持异步加载,更适合于浏览器环境,但也可以在 Node.js 中使用。 2. **使用方式:** **main.js** ```js // ES 模块 // 创建并暴露 函数方法 export const fn1 = () => console.log("我是方法1"); export const fn2 = () => console.log("我是方法2"); export const fn3 = () => console.log("我是方法3"); // 创建并暴露 数组 export const arr = [1, 2, 3, 4, 5, 6]; // 创建并暴露 字符串 export const strTest = "我是字符串"; // 创建并暴露 对象 export const obj = { name: "小明", age: 18, }; // 创建并暴露数值(default 最多只能存在一个,也可以省略没有) export const number = 666; export default number; ``` **app.js** ```js // ES 模块 // 把拿到的模块对象导入给对应的变量(解构) let { fn1, fn2, fn3, arr, strTest, obj, number } = await import( "./Utils/main.js" ); // 执行方法 fn1(); fn2(); fn3(); // 输出数组 console.log(arr); // 输出字符串 console.log(strTest); // 输出对象 console.log(obj); // 输出数值 console.log(number); ``` ![image-20250218154656402](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202502181546439.png)