2016-09-27 6 views
1

私は最近、ノードとデータベースのさまざまな部分で絶えず取り残されているので、ここではかなりの質問をしました。とにかくアプリケーションのパッケージ化後の電子とsqlite3の問題

、背景のビット:

私はAngularJSフロントエンドの電子アプリを持っています。だから、電子の側では、私は実際にipc上の電子と話すことができます私の角のアプリを提供する高速サーバーをスピンアップします。私はまた、データベースの処理(sqlite3)を行うために物事の明示的な側面を使用しています.Angelが$ httpを使用してヒットし、db結果が返されるAPIのルートを定義しています。私は 'npm start'でアプリを走らせている間、すべてのことは絶対にうまく動作します。次のようなもののDB /サーバ側のコードは次のとおりです。

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); 
}); 

問題:

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); 

としてだけでなく、その、ここにこのserver.jsファイルを必要とし、私のmain.jsファイルは、です私はアプリをパッケージ化するときにアプリケーションをパッケージ化するときに、アプリの機能は、サーバーがスピンアップし、角度コンポーネントが正常に動作しますが、私は読み取り/書き込み/私はパッケージ化する前のようにデータベースファイルを作成することはできません。私は今どこを見ても最終的に無知です!

+1

dbファイルに '__dirname'を使用しないでください。プロダクション用に' app.getPath( 'userData') 'という値が適しています。 –

+0

@MariusDarilaこれは、電子アプリケーションのパッケージ化時にファイルの作成やアクセスに違いがありますか? –

+0

データベース( 'app.db')が存在しない場合、実際にファイルを作成するため、ファイルを作成する関数への呼び出しを削除しました。私はまだこれを動作させることができません! –

答えて

1

私はしばらくの間私の机に頭を打ちつけた後、これをほぼ間違いなく修正することができました。私はいくつかのログを実装しようとしていましたが、ノードモジュールがパッケージ化されたとき正しく認識されないという問題があるかどうかを確認しようとしていました。実際のアプリディレクトリにはアクセスできませんでした。私のデータベースファイルでも同じことが起こりました。

は、以下の修正を参照してください:

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

問題を修正しましたDBファイルの定義に__dirnameの追加!

+3

@MariusDarilaは、ユーザがアプリケーションがインストールされているディレクトリに書き込む権限を持っていない場合、これが壊れてしまうことに言及しています。データベースファイルは、 'app.getPath( 'userData')' 。 –

関連する問題