{"id":19544451,"url":"https://github.com/dev-reading/practical-node-tutorial","last_synced_at":"2025-02-26T05:41:50.033Z","repository":{"id":150958835,"uuid":"109083741","full_name":"dev-reading/practical-node-tutorial","owner":"dev-reading","description":"实用Node.js，简单粗暴，新手学习的最短曲线","archived":false,"fork":false,"pushed_at":"2017-11-02T13:42:55.000Z","size":1295,"stargazers_count":86,"open_issues_count":0,"forks_count":9,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-01-08T19:19:53.233Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dev-reading.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-01T03:44:23.000Z","updated_at":"2022-07-29T15:06:10.000Z","dependencies_parsed_at":"2023-05-02T13:15:50.314Z","dependency_job_id":null,"html_url":"https://github.com/dev-reading/practical-node-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dev-reading%2Fpractical-node-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dev-reading%2Fpractical-node-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dev-reading%2Fpractical-node-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dev-reading%2Fpractical-node-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dev-reading","download_url":"https://codeload.github.com/dev-reading/practical-node-tutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240801037,"owners_count":19859727,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T03:28:57.478Z","updated_at":"2025-02-26T05:41:50.003Z","avatar_url":"https://github.com/dev-reading.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Practical Node Tutorial\n\n实用Node.js，简单粗暴，新手学习的最短曲线\n\n目录\n\n- 安装环境\n- 准备工作目录\n- 常用软件\n- 编辑器推荐VSCode\n- 学会VSCode调试\n- Mongo数据库\n- ...\n\n## 安装环境\n\n3m安装法\n\n- nvm（node version manager）【需要使用npm安装，替代品是yrm（支持yarn）】\n- nrm（node registry manager）【需要使用npm安装，替代品是yrm（支持yarn）】\n- npm（node packages manager）【内置，替代品是n或nvs（对win也支持）】\n\n### nvm\n\nnode版本发布非常快，而且多版本共存可能性较大，推荐使用nvm来安装node\n\n```shell\n$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh | bash\n\n$ echo 'export NVM_DIR=\"$HOME/.nvm\"' \u003e\u003e ~/.zshrc\n$ echo '[ -s \"$NVM_DIR/nvm.sh\" ] \u0026\u0026 . \"$NVM_DIR/nvm.sh\" # This loads nvm' \u003e\u003e ~/.zshrc\n$ source ~/.zshrc\n\n$ nvm install 0.10\n$ nvm install 4\n$ nvm install 6\n$ nvm install 8\n```\n\n### nrm\n\nhttps://registry.npmjs.com 是node官方的源（registry），服务器在国外，下载速度较慢，推荐安装nrm来切换源，国内的cnpm和taobao的源都非常快，当然，如果你想自建源也是支持的。\n\n```shell\n$ npm install --global nrm --registry=https://registry.npm.taobao.org\n$ nrm use cnpm\n```\n\n### npm\n\nnrm切换完源之后，你安装npm模块的速度会更快。\n\n```shell\n$ npm install --global yarn\n```\n\nnpm基本命令\n\n| 名称 | 描述 | 简写 |\n| --- | --- | --- |\n| npm install xxx | 安装xxx模块，但不记录到package.json里 | npm i xxx |\n| npm install --save xxx | 安装xxx模块，并且记录到package.json里，字段对应的dependency，是产品环境必须依赖的模块 | npm i -s xxx |\n| npm install --save-de xxx | 安装xxx模块，并且记录到package.json里，字段对应的dev-dependency，是开发环境必须依赖的模块，比如测试类的（mocha、chai、sinon、zombie、supertest等）都在 | npm i -D xxx |\n| npm install --global xxx | 全局安装xxx模块，但不记录到package.json里，如果模块里package.json有bin配置，会自动链接，作为cli命令 | npm i -g xxx |\n\n## 准备工作目录\n\n我的工作目录一般是 `~/workspace/github`\n\n## 常用软件\n\n- 1）oh my zsh是我最习惯的shell，终端下非常好用\n\n配合iterm2分屏 + spectacle全屏，几乎无敌\n\n- 2）brew是mac装软件非常好的方式，和apt-get、rpm等都非常类似\n\n安装4个必备软件\n\n- brew install git 最流行的SCM源码版本控制软件\n- brew install wget 下载、扒站神器\n- brew install ack  搜索代码神器\n- brew install autojump 终端下多目录跳转神器\n\n- 3）vim\n\n我虽然不算vim党，但也深爱着。janus是一个非常好用的vim集成开发环境。比如ctrl-p、nerdtree等插件都集成了，对我这种懒人足够了。\n\n![Vim](images/vim.png)\n\n## 编辑器推荐VSCode\n\nVisual Studio Code（以下简称vsc）\n\n- vsc是一个比较潮比较新的编辑器（跨平台Mac OS X、Windows和 Linux ）\n- vsc功能和textmate、sublime、notepad++，ultraedit等比较，毫不逊色\n- vsc尤其是在nodejs（调试）和typescript、go上支持尤其好\n- vsc提供了自定义 Debugger Adapter 和 VSCode Debug Protocol 从而实现自己的调试器\n\n值得一学\n\n下载安装VScode\n\n- 配置code命令\n- 配置快捷键\n- 安装vsconde-icons插件\n\n配置code命令\n\n![Vscode Code](images/vscode-code.png)\n\n配置快捷键，最喜欢cmd + [1-5]，这和xcode习惯一直，非常棒\n\n```\n// 将键绑定放入此文件中以覆盖默认值\n[\n    { \"key\": \"cmd+1\",           \"command\": \"workbench.view.explorer\" },\n    { \"key\": \"cmd+2\",           \"command\": \"workbench.view.search\" },\n    { \"key\": \"cmd+3\",           \"command\": \"workbench.view.scm\" },\n    { \"key\": \"cmd+4\",           \"command\": \"workbench.view.debug\" },\n    { \"key\": \"cmd+5\",           \"command\": \"workbench.view.extensions\" }\n]\n```\n\n安装vsconde-icons插件，对各种文件扩展都有icon显示，更直观\n\n![Vscode Icons](images/vscode-icons.png)\n\n## 学会VSCode调试\n\nexpress调试实例\n\n这是我们最常用的调试\n\n通过创建express项目构建，调试来演示vsc的具体用法\n\n### 创建express项目\n\n使用express-generator\n\n```\n➜  examples git:(master) ✗ express helloworld\n\n   create : helloworld\n   create : helloworld/package.json\n   create : helloworld/app.js\n   create : helloworld/public\n   create : helloworld/public/javascripts\n   create : helloworld/public/images\n   create : helloworld/public/stylesheets\n   create : helloworld/public/stylesheets/style.css\n   create : helloworld/routes\n   create : helloworld/routes/index.js\n   create : helloworld/routes/users.js\n   create : helloworld/views\n   create : helloworld/views/index.jade\n   create : helloworld/views/layout.jade\n   create : helloworld/views/error.jade\n   create : helloworld/bin\n   create : helloworld/bin/www\n\n   install dependencies:\n     $ cd helloworld \u0026\u0026 npm install\n\n   run the app:\n     $ DEBUG=helloworld:* npm start\n\n➜  examples git:(master) ✗ cd helloworld \n➜  helloworld git:(master) ✗ npm install\n➜  helloworld git:(master) ✗ npm start\n```\n\n测试express项目是正常的。\n\n说明：如果是自己的项目，需要自己构建git版本控制的，faq里有具体说明。\n\n### 修改launch.json的内容\n\n输入command + t快速定位文件：.vscode/launch.json\n\n修改launch.json的内容\n\n```\n{\n\t\"version\": \"0.1.0\",\n\t// List of configurations. Add new configurations or edit existing ones.\n\t// ONLY \"node\" and \"mono\" are supported, change \"type\" to switch.\n\t\"configurations\": [\n\t\t{\n\t\t\t// Name of configuration; appears in the launch configuration drop down menu.\n\t\t\t\"name\": \"Launch helloworld\",\n\t\t\t// Type of configuration. Possible values: \"node\", \"mono\".\n\t\t\t\"type\": \"node\",\n\t\t\t// Workspace relative or absolute path to the program.\n\t\t\t\"program\": \"examples/helloworld/bin/www\",\n\t\t\t// Automatically stop program after launch.\n\t\t\t\"stopOnEntry\": false,\n\t\t\t// Command line arguments passed to the program.\n\t\t\t\"args\": [],\n\t\t\t// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.\n\t\t\t\"cwd\": \".\",\n\t\t\t// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.\n\t\t\t\"runtimeExecutable\": null,\n\t\t\t// Optional arguments passed to the runtime executable.\n\t\t\t\"runtimeArgs\": [\"--nolazy\"],\n\t\t\t// Environment variables passed to the program.\n\t\t\t\"env\": {\n\t\t\t\t\"NODE_ENV\": \"development\"\n\t\t\t},\n\t\t\t// Use JavaScript source maps (if they exist).\n\t\t\t\"sourceMaps\": false,\n\t\t\t// If JavaScript source maps are enabled, the generated code is expected in this directory.\n\t\t\t\"outDir\": null\n\t\t},\n\t\t{\n\t\t\t\"name\": \"Attach\",\n\t\t\t\"type\": \"node\",\n\t\t\t// TCP/IP address. Default is \"localhost\".\n\t\t\t\"address\": \"localhost\",\n\t\t\t// Port to attach to.\n\t\t\t\"port\": 5858,\n\t\t\t\"sourceMaps\": false\n\t\t}\n\t]\n}\n```\n\n核心内容\n\n```\n\"name\": \"Launch helloworld\",\n\"type\": \"node\",\n\"program\": \"examples/helloworld/bin/www\",\n```\n\nprogram是要执行的express的入口。\n\n这里的helloworld是项目，所以找到/bin/www目录即可。\n\n### 点击调试按钮\n\n![](images/8.png)\n\n会弹出一个窗口，执行如下命令\n\n```\ncd '/Users/sang/workspace/github/vsc-doc'; env 'NODE_ENV=development'   'node' '--debug-brk=44412' '--nolazy' 'examples/helloworld/bin/www'\nDebugger listening on port 44412\n```\n\n其实node-inspector也是这个原理的。\n\n### 增加断点\n\n![](images/9.png)\n\n### 此时访问\n\n```\ncurl http://127.0.0.1:3200/\n```\n\n### 进入调试界面\n\n![](images/10.png)\n\n和chrome的调试是一样的。\n\n点击1）处按钮，打开控制台，配合调试，在控制台里查看对应的变量值\n\n另外值得说明的是二级菜单里4个部分\n\n- a）variables变量\n- b）watch观察\n- c）call stack 调用栈\n- d）break points 断点\n\n它和chrome的调试也是一样的，此处就不多讲了。\n\n### VSCode的其他调试方式\n\n- 当前文件\n- 附加到进程\n- 附加到远程服务器\n- 前端：推荐debugger for chrome插件\n- 其他语言调试\n\n课后作业：亲手debug一次，感受一下vsc的魅力\n\n### 更多\n\n- [node-debug 三法三例之node debugger + node inspector](https://github.com/i5ting/node-debug-tutorial/)\n    - 通过旧版本协议附加（node debug），Node.js 6.3-\n    - 通过检查器协议附加（v8-inspector），版本依赖是Node.js 6.3+\n- [node-inspector视频](http://i5ting.github.io/nodejs-video/node-inspector.mov)\n- [node-debug视频](http://i5ting.github.io/nodejs-video/node-debug.mov)\n\n更多vsc用法，参见 https://github.com/i5ting/vsc\n\n## Mongo数据库\n\n为什么要用mongodb？\n\n- 它是mean架构里的m（mongo）\n- 它是最像rdbms（mysql、oracle、db2）的nosql\n- 它的性能非常好，nosql天生优势，同时支持大数据处理和文件系统支持\n- 它对运维要求非常低，一般开发就可以\n- 它对事务支持不够好，但可以\n\t- 利用冗余，单个collection的事务处理做，宽表打扁\n\t- 通过二阶段法提交事务\n\n安装\n\n```\n$ brew install mongo \n```\n\n启动\n\n```\n$ npm i -g mh\n$ mh\n```\n\nmh内置2个命令\n\n- mh是当前目录启动，数据独立\n- mhg是在~下启动，共享数据\n\n最好的mongodb管理客户端\n\n- https://robomongo.org/（mac）\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdev-reading%2Fpractical-node-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdev-reading%2Fpractical-node-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdev-reading%2Fpractical-node-tutorial/lists"}