运行nodejs的程序,使用命令:node xxx.js,但是关掉终端,程序也关闭了,如何让node app的程序一直运行?
安装forever
npm install -g forever
使用forever开启nodejs程序
forever start xxx.js
如果不需要一直运行nodejs程序
forever stop xxx.js
关闭当前进程
control + c
杀掉进程
首先你要找到您的pid
ps gx | grep "node"
然后kill掉相关pid
kill pid
清楚 node.js 的 cache
sudo npm cache clean -f
安装node.js
确保mac上已安装homeBrew,使用下面命令安装node.js
brew install node
检测版本
node -v
node.js开发web
在目录/User/xxx/web下编写node.js的测试文件web.js,内容如下
var http = require('http');
var data = {key:'value',hello:'hello'};
var srv = http.createServer(function(req,res){
res.writeHead(200,{'Content-type':'application/json'});
res.end(JSON.stringify(data));
});
srv.listen(8080,function(){
console.log('listening on localhost:8080');
});
以上代码将监听8080端口的http请求
运行node.js
到web.js 目录文件夹下,执行以下命令
node web.js
浏览器访问查看返回
http://localhost:8080/
![](https://img.haomeiwen.com/i4637097/8728b340776484b0.png)
未完,待续~
网友评论