简体   繁体   English

Node.js和MySQL-JavaScript应用程序-如何与离线和在线数据库进行交互

[英]Node.js & MySQL - JavaScript application - how to interact with offline AND online databases

I am making a web-app using JavaScript. 我正在使用JavaScript制作网络应用。 I plan to use Node.js to connect the app to an existing MySQL database. 我计划使用Node.js将应用程序连接到现有的MySQL数据库。

First of all, will the Node code be written in the same .js file as my application? 首先,将节点代码与我的应用程序写入相同的.js文件中吗? Or is it a separate file? 还是单独的文件?

I need the data to be current at all times (even if you were to close the browser and re-open it, AND even in the event of the user not having a wifi connection), so my thought was to constantly update the local device's db and then to intermittently update the MySQL db. 我需要数据始终保持最新(即使您要关闭浏览器并重新打开它,甚至在用户没有wifi连接的情况下也是如此),所以我想一直在更新本地设备的数据db,然后间歇地更新MySQL db。 Is this the best strategy? 这是最好的策略吗? If so, how exactly can Node talk to the offline db and MySQL? 如果是这样,Node到底如何与离线db和MySQL通信?

First of all, will the Node code be written in the same .js file as my application? 首先,将节点代码与我的应用程序写入相同的.js文件中吗? Or is it a separate file? 还是单独的文件?

It is possible to keep your client side JavaScript in the same file as your server side JavaScript, but it doesn't make any sense to do so. 可以将客户端JavaScript与服务器JavaScript放在同一文件中,但是这样做没有任何意义。 They are separate programs. 它们是单独的程序。 (Library files, on the other hand, are a different story). (另一方面,图书馆文件则是另一回事)。

so my thought was to constantly update the local device's db and then to intermittently update the MySQL db. 所以我的想法是不断更新本地设备的数据库,然后间歇地更新MySQL数据库。

Working with a local database and syncing to a shared one is a common strategy. 一种常见的策略是使用本地数据库并同步到共享数据库。 You do need to handle conflicting updates in a way that is sensible for your purposes though. 但是,您确实需要以适合您的目的的方式处理冲突的更新。

If so, how exactly can Node talk to the offline db and MySQL? 如果是这样,Node到底如何与离线db和MySQL通信?

Node.js can't talk to the offline database, at least not directly. Node.js无法与脱机数据库通信,至少不能直接与之通信。

You will have a web application running in the browser. 您将在浏览器中运行一个Web应用程序。 It will use client side JavaScript with a client side database and some means of communicating with the server (often this is done by sending JSON over HTTP to and from a web service). 它将使用客户端JavaScript和客户端数据库以及与服务器进行通信的某种方式(通常通过与Web服务之间通过HTTP发送JSON来完成)。

Then you will have a server side application running in Node.js. 然后,您将在Node.js中运行一个服务器端应用程序。 It will use server side JavaScript with a server side MySQL database and some means of communicating with the client (ie an HTTP server hosting a web service). 它将使用服务器端JavaScript和服务器端MySQL数据库以及与客户端进行通信的某种方式(即托管Web服务的HTTP服务器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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