Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.html
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。
Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型,使其轻量又高效。
Node.js 的包管理器 npm,是全球最大的开源库生态系统。node
英文网址:https://nodejs.org/en/
中文网址:http://nodejs.cn/python
版本选择:【官方目前有两种版本,一种是长期稳定版,另外一种是开发版,推荐使用长期稳定版】linux
Long Term Support:长时间支持版本【简写LTS】npm
操做步骤:json
依赖python2.7centos
(一) 编译好的文件api
#下载 wget https://nodejs.org/dist/v8.5.0/node-v8.5.0-linux-x64.tar.xz #解压该文件 tar -xvf node-v8.5.0-linux-x64.tar.xz #存放路径 mv node-v8.5.0-linux-x64 /app/ ln -s node-v8.5.0-linux-x64 node #修改/etc/profile,添加环境变量 #SET PATH FOR NODEJS export NODE_HOME=/app/node export PATH=$NODE_HOME/bin:$PATH #使环境变量生效 source /etc/profile #运行node -v显示nodejs的版本号则安装配置成功 node -v
(二)经过源码编译bash
tar -zxvf node-v8.5.0.tar.gz cd node-v8.5.0 ./configure make make install cp /usr/local/bin/node /usr/sbin/ 查看当前安装的Node的版本 node -v v8.5.0
#安装pm2,pm2是node进程管理器 npm install -g pm2 #卸载pm2,须要删除家目录隐藏的目录.pm2 npm uninstall -g pm2 mv /root/.pm2 /tmp
测试app
#1. 添加测试文件helloworld.js var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n'); }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/'); #2. pm2启动: pm2 start helloworld.js --name 'helloworld' [root@120 www]# pm2 start helloworld.js --name 'helloworld' [PM2] Starting /app/www/helloworld.js in fork_mode (1 instance) [PM2] Done. ┌────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐ │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │ ├────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤ │ helloworld │ 0 │ fork │ 25376 │ online │ 0 │ 0s │ 28% │ 20.3 MB │ root │ disabled │ └────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘ Use `pm2 show <id|name>` to get more details about an app #3. 把pm2加入开机启动 pm2 startup centos [root@120 www]# pm2 startup centos [PM2] Init System found: systemd ----------------------------------------------------------- PM2 detected systemd but you precised centos Please verify that your choice is indeed your init system If you arent sure, just run : pm2 startup ----------------------------------------------------------- Platform centos Template [Unit] Description=PM2 process manager Documentation=https://pm2.keymetrics.io/ After=network.target [Service] Type=forking User=root LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity Environment=PATH=/app/node-v8.5.0-linux-x64/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin Environment=PM2_HOME=/root/.pm2 PIDFile=/root/.pm2/pm2.pid ExecStart=/app/node-v8.5.0-linux-x64/lib/node_modules/pm2/bin/pm2 resurrect ExecReload=/app/node-v8.5.0-linux-x64/lib/node_modules/pm2/bin/pm2 reload all ExecStop=/app/node-v8.5.0-linux-x64/lib/node_modules/pm2/bin/pm2 kill [Install] WantedBy=multi-user.target Target path /etc/systemd/system/pm2-root.service Command list [ 'chmod +x /etc/systemd/system/pm2-root.service', 'systemctl enable pm2-root', 'systemctl start pm2-root', 'systemctl daemon-reload', 'systemctl status pm2-root' ] [PM2] Writing init configuration in /etc/systemd/system/pm2-root.service [PM2] Making script booting at startup... >>> Executing chmod +x /etc/systemd/system/pm2-root.service [DONE] >>> Executing systemctl enable pm2-root Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-root.service to /etc/systemd/system/pm2-root.service. [DONE] >>> Executing systemctl start pm2-root [DONE] >>> Executing systemctl daemon-reload [DONE] >>> Executing systemctl status pm2-root ● pm2-root.service - PM2 process manager Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: disabled) Active: active (running) since 一 2017-12-11 11:49:13 CST; 142ms ago Docs: https://pm2.keymetrics.io/ Main PID: 25245 (PM2 v2.8.0: God) CGroup: /system.slice/pm2-root.service ‣ 25245 PM2 v2.8.0: God Daemon (/root/.pm2) 12月 11 11:49:13 120.234.2.74_centos7 pm2[25587]: [PM2] Restoring processes located in /root/.pm2/dump.pm2 12月 11 11:49:13 120.234.2.74_centos7 pm2[25587]: [PM2][ERROR] Failed to read dump file in /root/.pm2/dump.pm2.bak 12月 11 11:49:13 120.234.2.74_centos7 pm2[25587]: [PM2][ERROR] No processes saved; DUMP file doesn't exist 12月 11 11:49:13 120.234.2.74_centos7 pm2[25587]: ┌────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐ 12月 11 11:49:13 120.234.2.74_centos7 pm2[25587]: │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │ 12月 11 11:49:13 120.234.2.74_centos7 pm2[25587]: ├────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤ 12月 11 11:49:13 120.234.2.74_centos7 pm2[25587]: │ helloworld │ 0 │ fork │ 25376 │ online │ 0 │ 3m │ 0% │ 21.6 MB │ root │ disabled │ 12月 11 11:49:13 120.234.2.74_centos7 pm2[25587]: └────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘ 12月 11 11:49:13 120.234.2.74_centos7 pm2[25587]: Use `pm2 show <id|name>` to get more details about an app 12月 11 11:49:13 120.234.2.74_centos7 systemd[1]: Started PM2 process manager. [DONE] +---------------------------------------+ [PM2] Freeze a process list on reboot via: $ pm2 save [PM2] Remove init script via: $ pm2 unstartup centos #4. 保存更改 [root@120 www]# pm2 save
PM2经常使用命令
# 启动命令,推荐使用pm2 run npm 方式启动 # 参考连接:https://newsn.net/say/node-pm2-start.html $ pm2 run npm -start --name invoice # invoice别名 $ pm2 start app.js # 启动app.js应用程序 # 4个应用程序会自动进行负载均衡 $ pm2 start app.js -i 4 # cluster mode 模式启动4个app.js的应用实例 $ pm2 start app.js --name="api" # 启动应用程序并命名为 "api" $ pm2 start app.js --watch # 当文件变化时自动重启应用 $ pm2 start script.sh # 启动 bash 脚本 $ pm2 list # 列表 PM2 启动的全部的应用程序 $ pm2 monit # 显示每一个应用程序的CPU 和内存占用状况 $ pm2 show [app-name] # 显示应用程序的全部信息 $ pm2 logs # 显示全部应用程序的日志 $ pm2 logs [app-name] # 显示指定应用程序的日志 $ pm2 flush $ pm2 stop all # 中止全部的应用程序 $ pm2 stop 0 # 中止 id为 0的指定应用程序 $ pm2 restart all # 重启全部应用 $ pm2 reload all # 重启 cluster mode下的全部应用 $ pm2 gracefulReload all # Graceful reload all apps in cluster mode $ pm2 delete all # 关闭并删除全部应用 $ pm2 delete 0 # 删除指定应用 id 0 $ pm2 scale api 10 # 把名字叫api的应用扩展到10个实例 $ pm2 reset [app-name] # 重置重启数量 $ pm2 startup # 建立开机自启动命令 $ pm2 save # 保存当前应用列表 $ pm2 resurrect # 从新加载保存的应用列表 $ pm2 update # Save processes, kill PM2 and restore processes $ pm2 generate # Generate a sample json configuration file
yarn和npm对比:
1、yarn.lock 文件 在 npm 中一样可使用 npm shrinkwrap 命令来生成一个锁文件,这样在使用 npm install 时会在读取 package.json 前先读取这个文件,就像 Yarn 会先读取yarn.lock 同样。这里的区别是 Yarn 总会自动更新 yarn.lock,而 npm 须要你从新操做。 2、并行安装 每当 npm 或 Yarn 须要安装一个包时,它会进行一系列的任务。在 npm 中这些任务是按包的顺序一个个执行,这意味着必须等待上一个包被完整安装才会进入下一个;Yarn 则并行的执行这些任务,提升了性能。 3、清晰的输出 npm 默认状况下很是冗余,例如使用 npm install 时它会递归列出全部安装的信息;而 Yarn 则一点也不冗余,当可使用其它命令时,它适当的使用 emojis 表情来减小信息(Windows 除外)
正式使用:
1、安装yarn 打开命令行输入并执行下面指令安装yarn包管理工具 npm i yarn -g --verbose 2、更换源地址 npm官方源虽然在国内没有被墙,但访问速度仍然不敢恭维,所以次建议使用以前切换为淘宝镜像,在yarn安装完毕以后执行以下指令: yarn config set registry https://registry.npm.taobao.org 到此为止咱们就能够在项目中像使用npm同样使用yarn了。
经常使用指令:
yarn / yarn install 等同于npm install 批量安装依赖 yarn add xxx 等同于 npm install xxx —save 安装指定包到指定位置 yarn remove xxx 等同于 npm uninstall xxx —save 卸载指定包 yarn add xxx —dev 等同于 npm install xxx —save-dev yarn upgrade 等同于 npm update 升级所有包 yarn global add xxx 等同于 npm install xxx -g 全局安装指定包
总结:
在批量安装包文件时,yarn的安装速度远胜于npm,可是相对于老牌npm,yarn目前仍然不是很稳定,偶尔会致使包安装出现问题,你们能够根据我的状况来决定是否使用。
# 安装依赖库 cd 项目目录 npm i # 构建 npm run build # 启动nodejs程序 cd 项目目录 pm2 start dist/index.js 启动命令,若是研发添加了server命令: npm run server # 删除pm2任务 pm2 delete ID号 # 删除所有任务 pm2 delete all # 重启pm2任务 pm2 restart ID号 # 查看启动日志 pm2 logs
yarn经常使用命令
# 安装依赖 yarn