使用 node-Windows 启动 Windows 服务时出现问题

Having problem with starting a windows services using node-Windows

提问人:JEFF BRO 提问时间:10/27/2023 更新时间:10/27/2023 访问量:18

问:

我正在尝试启动一个 Windows 服务,用于在后台从 API 获取数据,并在有互联网连接时更新本地 SQLite 数据库。首先,我创建了一个脚本,该脚本调用Api并从中获取数据并创建一个文本文件。我正在尝试通过运行节点文件来启动服务,我在其中链接了脚本文件和所有其他必要的详细信息以安装和启动服务。我能够安装服务并在 Windows 服务列表中查看我的服务名称。

服务业

即使手动运行服务后,链接的脚本也不会运行。

这是我安装和启动脚本的代码

BgSync.js

const Service = require('node-windows').Service;
const fs = require('fs');

const svc = new Service({
    name: 'MondialAutoSync',
    description: 'Auto Sync for DB',
    script: "D:\WindowService\SyncserviceApi.js" 
});


svc.on('install', function () {
    fs.writeFile('Test.txt', "TEsing", function (err) {
        if (err) throw err;
        console.log('Saved!');
      });
    try {
        svc.start();
    } catch (error) {
        fs.writeFile('Errors.txt', error.stack, function (err) {
            if (err) throw err;
            console.log('Error saved in Errors.txt');
        });
    }
});

svc.on('uninstall', function () {
    console.log('Uninstall complete.');
});

svc.on('error', function (error) {
    fs.writeFile('ServiceError.txt', error.stack, function (err) {
        if (err) throw err;
        console.log('Service error saved in ServiceError.txt');
    });
});


svc.install();
// svc.start();

这是我的实际剧本


const axios = require('axios')
var fs = require('fs');
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('database.db');



fs.writeFile('Test.txt', "TEsing", function (err) {
  if (err) throw err;
  console.log('Saved!');
});

function insertData(result) {
  // Insert data into the SQLite UserMaster table
  const insertSql = `INSERT INTO electrondb (UserName, Password, FirstName, LastName, Email, Address1, Address2, UserCode, CountryId, CountyId, CityId, StateId, ZipId, Phone, RemoteAccessPassword, SecondPassword, UserGroupId, LastLogin, LastIPAddress, LastBrowser, CustomerId, BranchId, CreationDate, CreatedBy, ModifiedDate, ModifiedBy, Active, OwnerORSalesman, Loginid, PushPinPath1, PushPinPath2, PushPinPath3, PushPinPath4, PushPinPath5, DefaultWarehouseId, Initial, UserClassId, UpdatePwd, LastlogoutTime , UserId) VALUES (?, ?, ?, ?, ?,?,?,?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
  `;

  result.forEach((row) => {
    // console.log(row)
    db.run(
      insertSql,
      row.UserName,
      row.Password,
      row.FirstName,
      row.LastName,
      row.Email,
      row.Address1,
      row.Address2,
      row.UserCode,
      row.CountryId,
      row.CountyId,
      row.CityId,
      row.StateId,
      row.ZipId,
      row.Phone,
      row.RemoteAccessPassword,
      row.SecondPassword,
      row.UserGroupId,
      row.LastLogin,
      row.LastIPAddress,
      row.LastBrowser,
      row.CustomerId,
      row.BranchId,
      row.CreationDate,
      row.CreatedBy,
      row.ModifiedDate,
      row.ModifiedBy,
      row.Active,
      row.OwnerORSalesman,
      row.Loginid,
      row.PushPinPath1,
      row.PushPinPath2,
      row.PushPinPath3,
      row.PushPinPath4,
      row.PushPinPath5,
      row.DefaultWarehouseId,
      row.Initial,
      row.UserClassId,
      row.UpdatePwd,
      row.LastlogoutTime,
      row.UserId,
      
      (insertErr) => {
        if (insertErr) {
          console.error("Error inserting data:", insertErr);
        } else {
          console.log("Data inserted into UserMaster table");
        }
      }
    );
  });

}


function FetchAllData () {
  return new Promise((resolve, reject) => {
    try {
      const query = 'SELECT * FROM electrondb';  
      db.all(query, (err, rows) => {
        if (err) {
          console.error('Error executing SQL query:', err);
          reject(err);
        } else {
          console.log(rows);
          resolve(rows);
        }
      });
    } catch (err) {
      console.error('Error executing SQL:', err);
      reject(err);
    }
  });
}

  let head = {
    RequestKey: '***************0w==',
    "Content-Type": "application/json" ,
    // "Access-Control-Allow-Origin:": "http://localhost:3000"
}
  const requestOptions = {
    
    headers: head,
  };
  

  function compareArrays(array1, array2) {

    const mapArray1 = new Map();
  
    array1.forEach((obj) => {
      
      const key = obj.UserId; 
      mapArray1.set(key, obj);
    });
  
    
    const differentObjects = array2.filter((obj2) => {
      const key = obj2.UserId; 
      return !mapArray1.has(key);
    });
  // console.log( "Different", differentObjects)
    return differentObjects
  }
  

 
  
  

 

  return axios
    .get('https://****/****/mic*****1/g*****r', { headers: requestOptions.headers })
    .then(async(results) => {
        // console.log(results.data.Table)
        let LocalData = await FetchAllData()
      let DataFrmApi = results.data.Table
      if(LocalData && DataFrmApi) {
        const result = compareArrays(LocalData, DataFrmApi);
        console.log("Different",result)
        insertData(result)
      }
  

      


      // fs.writeFile('mynewfile3.txt', JSON.stringify(results.data.Table[0]), function (err) {
      //   if (err) throw err;
      //   console.log('Saved!');
      // });
 
      return results;
    })
    .catch((error) => {
      console.log("Error in Get Method " + error);
      fs.writeFile('Errors.txt', error, function (err) {
        if (err) throw err;
        console.log('Saved!');
      });
 
   
    });



这是我的代码.

节点 .js 窗口 窗口 服务 桌面 应用程序 节点 窗口

评论


答: 暂无答案