[英]Electron and sqlite3 issue after packaging app
我最近在這里問了很多問題,因為我不斷地被節點和數據庫的不同部分所困擾。
無論如何,有點背景:
我有一個帶有 AngularJS 前端的 Electron 應用程序。 所以在電子方面,我實際上啟動了一個快速服務器,它為我的 angular 應用程序提供服務,然后它當然可以通過 ipc 與電子通信。 我還使用快速的一面來做數據庫的東西(sqlite3),為一個 api 定義路由,Angular 可以使用 $http 命中該路由,並以這種方式返回 db 結果。 當我使用“npm start”運行應用程序時,一切正常。 db/server 端的代碼如下:
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打包我的應用程序時,應用程序運行,服務器啟動並且 Angular 組件工作正常,但我無法閱讀/寫/創建一個數據庫文件,就像我在打包之前一樣。 我現在最終不知道去哪里看!
在我的頭撞在我的桌子上一段時間后,我幾乎是偶然地設法解決了這個問題。 我試圖實現一些日志記錄,以查看打包時是否存在無法正確識別節點模塊的問題,當我注意到日志文件沒有在我期望的位置創建時 - 它是在外部創建的實際應用程序目錄,因此無法訪問。 我的數據庫文件也發生了同樣的事情。
請參閱下面的修復:
var db = new sqlite3.Database(__dirname + '/app.db');
將 __dirname 添加到 db 文件定義解決了這個問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.