1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| /** IMPORT **/
var Service = require('node-windows').Service,
EventLogger = require('node-windows').EventLogger,
logger = new EventLogger('Hello World');
const fs = require('fs');
WriteLog('Imports done');
/** GLOBAL VAR **/
var fileName = 'nodeService.log';
/** Service config **/
WriteLog('Service configuration :');
var svc = new Service({
name:'Hello Service',
description: 'NodeJs service test',
script: 'D:\\dev\\nodejs\\app.js'
});
WriteLog('Initialization OK');
/*
svc.logOnAs.account = 'AdminDev';
svc.logOnAs.password = '***********';
*/
WriteLog('Credentials OK');
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
WriteLog('Starting service...');
svc.start();
WriteLog('Started');
});
WriteLog("'install' trigger OK");
// Listen for the "start" event and let us know when the
// process has actually started working.
svc.on('start',function(){
logger.info('Le service a demarrer');
console.log(svc.name+' started!');
});
WriteLog("'start' trigger OK");
// Install and run
WriteLog('Starting install...');
svc.install();
function WriteLog(log) {
var timing = new Date(),
log = timing + ' - install.js | ' + log + '\r\n';
fs.appendFile('service.log', log, (err) => {
// throws an error, you could also catch it here
if(err) {
logger.error('Error in Logger : \n' + err);
console.log(err);
throw err;
}
});
} |
Partager