电子和sqlite3问题打包后的应用程序

最近我在这里问了很多问题,因为我一直在困扰着节点和数据库的不同部分。

无论如何,一点背景:

我有一个AngularJS前端的电子应用程序。 所以在电子方面,我实际上启动了一个服务于我的angular度应用程序的快速服务器,这当然可以通过ipc与电子交谈。 我也使用事物的expression方式来做数据库的东西(sqlite3),定义一个api哪些Angular可以使用$ http命中路线,并有数据库结果返回的方式。 当我使用“npm start”运行应用程序时,一切正常。 数据库/服务器端的代码如下所示:

var path = require('path'); var express = require('express'); var app = express(); var fs = require('fs'); var bodyParser = require('body-parser'); var path = require('path'); var sqlite3 = require('sqlite3').verbose(); // Load the db function createDbFile() { // Try to open the db file - if it doesn't exist, create it. try { var filebuffer = fs.readFileSync(path.join(__dirname, 'app.db')); } catch (err) { if (err.code === 'ENOENT') { fs.closeSync(fs.openSync(path.join(__dirname, 'app.db'), 'w')); } else { throw err; } } } createDbFile(); var db = new sqlite3.Database('app.db'); var check; db.serialize(function() { db.run("CREATE TABLE IF NOT EXISTS lorem (info TEXT)"); var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); for (var i = 0; i < 10; i++) { stmt.run("Ipsum " + i); } stmt.finalize(); }); app.use(express.static(__dirname)); // app.use(bodyParser.json()); app.get('/', function (req, res) { res.sendFile(__dirname + 'index.html'); }); app.get('/loading', function (req, res) { res.sendFile(__dirname + '/loading.html'); }); app.get('/api/get/all', function (req, res) { db.all("SELECT * FROM lorem", function(err, row) { res.json(row); }); }); app.post('/api/post/site', function (req, res) { // Do stuff here. }); app.listen(3333); 

以及那,这是我的main.js文件,它需要这个server.js文件:

 const electron = require('electron'); const server = require("./server"); const ipcMain = require('electron').ipcMain; const app = electron.app; const BrowserWindow = electron.BrowserWindow; // Define our global references let mainWindow, loadingScreen, windowParams = { width: 1000, height: 700, show: false }, loadingWindowParams = { width: 400, height: 300, show: false, frame: false }; // Define our loading window whose parent is main function createLoadingScreen() { loadingScreen = new BrowserWindow(Object.assign(loadingWindowParams, {parent: mainWindow})); loadingScreen.loadURL('http://localhost:3333/loading'); loadingScreen.on('closed', () => loadingScreen = null); loadingScreen.webContents.on('did-finish-load', () => { loadingScreen.show(); }); } app.on('ready', () => { // Create loading screen createLoadingScreen(); // Create the browser window. mainWindow = new BrowserWindow(windowParams); // Point to our express server mainWindow.loadURL(`http://localhost:3333`); // Open the DevTools. mainWindow.webContents.openDevTools(); // Simulate loading to allow angular to initialize, then show main window mainWindow.once('ready-to-show', () => { if (loadingScreen) { setTimeout(function() { loadingScreen.close(); mainWindow.show(); }, 6000); } }); // Close the app after window closed for security purposes mainWindow.on('closed', function () { mainWindow = null app.quit(); }); // Handle messages ipcMain.on('electron-msg', (event, msg) => { switch (msg.type) { case 'system': mainWindow.webContents.send('electron-msg', 'Message received'); break; } }); }); // Quit when all windows are closed. app.on('window-all-closed', function () { if (process.platform !== 'darwin') { app.quit() } }); // Open window again when activated app.on('activate', function () { if (mainWindow === null) { // Not currently needed, we quit when window closed } }); // Throw our errors process.on('uncaughtException', function (err) { console.log(err); }); 

我遇到的问题是,当我打包我的应用程序使用https://github.com/electron-userland/electron-builder ,应用程序的function,服务器启动和angular组件工作正常,但我无法阅读/写/创build一个数据库文件就像我可以在打包之前。 我现在终于无能为力了!

我已经设法解决这个几乎意外后,我的头撞在我的办公桌一段时间。 我试图执行一些日志logging,看看是否有问题,没有正确识别节点模块打包时,当我注意到日志文件没有被创build在我期待它是 – 它创build之外实际的应用程序目录,因此无法访问。 同样的事情发生在我的数据库文件。

看到下面的修复:

 var db = new sqlite3.Database(__dirname + '/app.db'); 

__dirname添加到数据库文件定义解决了问题!