1、安装nodejs
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install);"
brew install node
测试
node -v
npm -v
2、安装pm2 (node进程管理器)
npm install -g pm2
pm2常用命令
$ pm2 start app.js -i 4 # 后台运行pm2,启动4个app.js
# 也可以把'max' 参数传递给 start
# 正确的进程数目依赖于Cpu的核心数目
$ pm2 start app.js --name my-api # 命名进程
$ pm2 list # 显示所有进程状态
$ pm2 monit # 监视所有进程
$ pm2 logs # 显示所有进程日志
$ pm2 stop all # 停止所有进程
$ pm2 restart all # 重启所有进程
$ pm2 reload all # 0 秒停机重载进程 (用于 NETWORKED 进程)
$ pm2 stop 0 # 停止指定的进程
$ pm2 restart 0 # 重启指定的进程
$ pm2 startup # 产生 init 脚本 保持进程活着
$ pm2 web # 运行健壮的 computer API endpoint (http://localhost:9615)
$ pm2 delete 0 # 杀死指定的进程
$ pm2 delete all # 杀死全部进程
测试
pm2 -v
3、调试demo
编辑js文件 helloworld.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n');
}).listen(9898, "127.0.0.1");
console.log('Server running at http://127.0.0.1:9898/');
启动
pm2 start helloworld.js --name 'helloworld'
测试
http://localhost:9898/
参考资料:
http://www.expressjs.com.cn/starter/static-files.html
http://www.4u4v.net/from-scratch-using-react-webpack-nodejs-express-rapid-construction-project.html
http://www.jianshu.com/p/42e11515c10f