node要自啟動需要pm2工具,還有其他工具可以自行選擇
先搭建好node.js&pm2生產(chǎn)環(huán)境,http://www.itdecent.cn/p/43525232b03b
安裝pm2:npm install -g pm2
查看pm2版本:pm2 -v
查看服務(wù)列表:pm2 list
啟動服務(wù):pm2 start 你的文件
pm2示例
下邊就可以做自啟動和開機啟動了。
編寫一個pm2啟動服務(wù)的批處理文件
@echo off
pm2 start D:\node\helloworld\bin\www D:\node\serviceDemo.js
我這里是同時啟動上邊示例圖片中的兩個服務(wù)。
將寫好的批處理文件放在下邊的路徑下,就會開機自啟了
C:\Users\當(dāng)前登錄用戶名\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
批處理文件放置位置
serviceDemo.js文件,網(wǎng)上隨便一搜就是這個代碼
var http = require('http');
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'text/plain'})
response.end('Hello world\n');
}).listen(8888);
console.log('Server runnint at http:localhost:8888');
如果修改了文件需要手動重啟服務(wù)
運行效果:
運行效果