简体   繁体   中英

Need help for my thesis project: electron, express & sqlite3

i'm currently working on my thesis project which consists of an educational desktop videogame for public and private schools in my city.

I also really like electron so i thought it would be a nice idea to make my app using it, however it's here when the problem starts. My university demands that all apps must use a relational database (SQL) and not non-relational db like MongoDB. Since the database i'll have it's relative small i chose SQLite.

For some reason i began to get a certain error:

C:\Users\Alejandro\Documents\Proyectos\express\node_modules\sqlite3\lib\binding\electron-v1.4-win32-ia32\node_sqlite3.node

After some research i found out that the reason was because electron executes on the client side and databases can only work on server side, to solve this issue i installed express on my app to execute a server in the background while electron executes the client (local client-server desktop app).

After some coding this was the result:

  • Main.js (Electron code changes)

     const express = require('./resources/express') const electron = require('electron') // Module to control application life. const app = electron.app // Module to create native browser window. const BrowserWindow = electron.BrowserWindow const path = require('path') const url = require('url') // Keep a global reference of the window object, if you don't, the window will be closed automatically when the JavaScript object is garbage collected. let mainWindow function createWindow () { // Create the browser window. mainWindow = new BrowserWindow({width: 800, height: 600}) // and load the index.html of the app. mainWindow.loadURL('http://localhost:8080/') // Emitted when the window is closed. mainWindow.on('closed', function () { console.log('app server is closing') express.closeServer() mainWindow = null }) } 
  • Express.js (Express code):

     const http = require('http') const express = require('express') const dbmanager = require('./dbmanager.js') app = express() app.set('view engine', 'pug') app.get('/',function(request, response){ console.log('server started :D') response.end() }) app.get('/checktable',function(request, response){ dbmanager.createTable() response.redirect('/receive') }) app.get('/receive',function(request, response){ dbmanager.selectAll(function(err,data){ data.forEach(function(row){ console.log(row.id + " " + row.name + "Edad: " + row.age) }) response.render('index', { title: 'Usuario', message: data[0].name + " " + data[0].last }) response.end() }) }) var server = http.createServer(app).listen(8080) var serverManager = {} serverManager.closeServer = function(){// This executes console.log('This is executing') console.log('Server should close now') server.close() } module.exports = serverManager 
  • dbmanager.js (SQLite queries)

     var sqlite3 = require('sqlite3').verbose() var db = new sqlite3.Database('memory') var queryManager = {} queryManager.createTable = function(){ db.run("CREATE TABLE IF NOT EXISTS students (id int, name TEXT,last TEXT, age int)") } queryManager.deleteTable = function(){ db.run('DROP TABLE students') } queryManager.insertStudent = function(student){ var stmt = db.prepare("INSERT INTO students VALUES (?,?,?,?)") stmt.run(student.id,student.name,student.last,student.age) stmt.finalize() console.log('Insert Successful') } queryManager.selectAll = function(callback){ db.all('SELECT * FROM students', function(err,rows){ if(err) { throw err; } else { callback(null, rows); } }) } module.exports= queryManager 
  • Index.pug (view)

      html head title= title body table#table h1= message 

Before trying to execute the entire app i tried only executing the server and it worked.

I modified the npm start line from "electron ." to "electron . && node ./resources/express.js" so i could execute both the client and the server.

At that moment i commented all the lines related to the dbmanager.js to test if the client and server were working.

Needing to close the server at the moment the app windows was close i created a function to close the server when window close funcion is called, but it doesn't.

That's the first problem. The second problem shows up when i undo the comment on the dbmanager lines, i get the same error as before:

 'C:\Users\Alejandro\Documents\Proyectos\express\node_modules\sqlite3\lib\binding\electron-v1.4-win32-ia32\node_sqlite3.node'

Am i doing something wrong? Please i really need help.

You're spinning up an Express server on the same machine as your Electron app, if your server can access a local database then what makes you think your app can't? You don't need an Express server. To use the sqlite3 module in an Electron app you just have to rebuild it to target the specific Electron version using one of the documented approaches .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM