問題描述
我知道我可以使用 HTML CSS JavaScript 或任何其他前端框架來設計 UI.我什至明白我可以連接到任何遠程 API.如果我想要一個帶有數(shù)據庫的獨立應用程序怎么辦.我應該如何連接到數(shù)據庫?我應該在哪里寫我的應用程序邏輯?我覺得電子中有一個缺失的部分我應該使用像 Express 這樣的節(jié)點 JS Web 框架嗎?或者我應該在不使用任何框架的情況下用純節(jié)點 JS 編寫所有程序邏輯?如果可能的話,編寫電子應用程序的最佳方法是什么,請指出任何工作示例.
I understand I can design the UI with HTML CSS JavaScript or any other frontend framework. I even understand that I can connect to any remote API. what if I want a standalone application with database. How should I connect to the database ? where should I write my application logic? I feel like there is a missing part in electron do I supposed to use a node JS web Framework like Express? or I should write all the program logics in pure node JS without using any framework? What is the best approach to write electron applications if possible please point me any working example.
推薦答案
這完全取決于你.
雖然我們在 Web 應用程序中習慣的客戶端/服務器(前端/后端)模型是一個好主意(關注點分離),但當客戶端和服務器在同一臺機器.
While the client/server (frontend/backend) model we've got used to in web applications is a good idea (separation of concerns), it's not the only way to do things when the client and the server are on the same machine.
Electron 構建在 Node.js 之上.因此,您可以使用通常的 npm 模塊來連接到您想要使用的任何數(shù)據庫系統(tǒng)并取消框架.例如,如果您愿意,您可以編寫代碼以將數(shù)據從數(shù)據庫直接提取到您的 onclick
事件處理程序中.
Electron is built on top of Node.js. So you can use the usual npm modules in order to connect to whatever database system you want to use and do away with frameworks. For example you can write code to fetch data from the database right into your onclick
event handler if you desire so.
話雖如此,如果您不小心,您很可能會發(fā)現(xiàn)自己正在處理一堆難以管理的意大利面條代碼.因此,即使您不想要整個客戶端/服務器系統(tǒng), 種結構也是推薦的.
Having said that, odds are you will find yourself dealing with an unmanageable bunch of spaghetti code if you're not careful. So, some kind of structure is recommended even if you don't want an entire client/server system.
此外,您的客戶端"和服務器"不必通過 HTTP 進行通信.接口可以只是普通的函數(shù)(和/或方法)調用.Electron 還有一個消息傳遞系統(tǒng)(例如:https://electronjs.org/docs/api/ipc-main 你可以使用.
Also, your "client" and your "server" don't have to communicate through HTTP. The interface can be just plain function (and/or method) calls. Electron also has a message passing system (for example: https://electronjs.org/docs/api/ipc-main that you may use.
這篇關于我應該在電子應用程序中使用什么作為后端?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!