我目前正致力于我的论文项目,该项目包括我所在城市的公立和私立学校的教育桌面视频游戏。
我也非常喜欢电子,所以我认为使用它来制作我的应用程序是一个不错的主意,但是当问题开始时它会在这里。我的大学要求所有应用程序必须使用关系数据库(SQL)而不是像MongoDB这样的非关系数据库。由于数据库我的相对较小,我选择了SQLite。
出于某种原因,我开始遇到一个错误:
C:\Users\Alejandro\Documents\Proyectos\express\node_modules\sqlite3\lib\binding\electron-v1.4-win32-ia32\node_sqlite3.node
经过一些研究后我发现原因是因为电子在客户端执行而数据库只能在服务器端工作,为了解决这个问题,我在我的app上安装了express来在后台执行服务器,而电子执行客户端(本地客户端 - 服务器桌面应用程序)。
经过一些编码,这就是结果:
Main.js(电子代码更改)
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(快递代码):
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查询)
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
在尝试执行整个应用程序之前,我尝试只执行服务器并且它有效。
我修改了"电子的npm起始线。"到了电子。 &安培;&安培; node ./resources/express.js"所以我可以执行客户端和服务器。
在那一刻,我评论了与dbmanager.js相关的所有行,以测试客户端和服务器是否正常工作。
在应用程序窗口关闭时需要关闭服务器我创建了一个函数来在调用窗口关闭函数时关闭服务器,但它没有。
这是第一个问题。当我撤消对dbmanager行的注释时出现第二个问题,我得到与以前相同的错误:
'C:\Users\Alejandro\Documents\Proyectos\express\node_modules\sqlite3\lib\binding\electron-v1.4-win32-ia32\node_sqlite3.node'
我做错了吗?我真的需要帮助。
答案 0 :(得分:0)
如果您的服务器可以访问本地数据库,那么您在与Electron应用程序相同的计算机上启动Express服务器,那么是什么让您认为您的应用程序无法访问?您不需要Express服务器。要在Electron应用中使用temp
模块,您只需重建它以使用documented approaches之一定位特定的Electron版本。