2018年9月20日 星期四

讓 nodejs 跑 service

編輯 /etc/systemd/system/nodejs.service
[Unit]
Description=nodejs server

[Service]
ExecStart=/usr/bin/node /var/nodejs/index.js
Restart=always
User=nobody
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/nodejs

[Install]
WantedBy=multi-user.target

編輯測試程式 /var/nodejs/index.js 內容
var http = require('http');

var hostname = '10.10.10.10';
var port = 3849;

http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World');
}).listen(port, hostname, function () {
console.log('Server running at http://%s:%s/', hostname, port);
});

重新載入所有service
systemctl daemon-reload

啟動 nodejs service
systemctl start nodejs

用lynx連結 http://10.10.10.10:3849,網頁顯示 Hello World 表示成功

設定自動啟動 nodejs
systemctl enable nodejs

沒有留言:

解決無法連線不需認證的遠端資料匣

最近User要連事務機資料匣,發現無法連線,錯誤訊息如下:  Fix You can’t access this shared folder because your organization’s security policies block unauthenticated 解...