简体   繁体   English

如何将实时数据集成到nodeJS并表达

[英]How to integrate live data to nodeJS and express

I'm using express to deal with all the routing of my web app. 我正在使用快递来处理我的网络应用程序的所有路由。 I'm also using firebase to store all my data. 我也使用firebase来存储我的所有数据。

So I wanted to split my code like this: 所以我想像这样分割我的代码:

index.js : node app, routing and get data from firebase. index.js :节点应用,路由和从firebase获取数据。 Send data to the view(s). 将数据发送到视图。

index.html : display my app (a view) and include client.js index.html :显示我的应用程序(视图)并包含client.js

client.js : use the data from index.js (firebase) and use it to generate a graph in index.html . client.js :使用index.js (firebase)中的数据并使用它在index.html生成图形。

I liked this structure because index.js was in charge on the routing and getting the data, and index.html and client.js handled the display. 我喜欢这个结构,因为index.js负责路由并获取数据, index.htmlclient.js处理了显示。

Unfortunately, this structure is not really working with Firebase. 不幸的是,这种结构并不适用于Firebase。 Since Firebase receive data at each modification of the DB, this code: 由于Firebase在每次修改DB时都会收到数据,因此代码如下:

app.get('/moods/get', function(req, res){
    moodRef.on('value', function(snapshot){
        res.json(snapshot.val());
    })
});

Display this error: 显示此错误:

Error: Can't set headers after they are sent.

I think it's pretty obvious, the HTML page is already displayed, so trying to send json produces this error. 我认为很明显,HTML页面已经显示,因此尝试发送json会产生此错误。

So what is the solution? 那么解决方案是什么? Using firebase in client.js so it can update in live the display, but my code structure becomes a bit weird. client.js使用firebase,因此它可以在实时更新显示,但我的代码结构变得有点奇怪。

client.js is now in charge of the data, the display, and index.js is only in charge of the routing? client.js现在负责数据,显示和index.js只负责路由?

What are the best practices? 什么是最佳做法? How would you organize your code for this kind of projects? 你会如何组织这类项目的代码?

Thanks in advance. 提前致谢。

With http, you cannot just send data to the client whenever you want. 使用http,您无法随时将数据发送到客户端。 The client has to make a specific request and then you can return data from that request and only for the life of that request. 客户端必须发出特定请求,然后您可以从该请求返回数据,并且仅在该请求的生命周期内返回。

So, for http, you would have to field the http request to get some data and then do a query of the database to fetch the appropriate data that matches what the specific http request is asking for. 因此,对于http,您必须对http请求进行字段处理以获取一些数据,然后对数据库进行查询以获取与特定http请求所要求的匹配的相应数据。

If you want to be able to just send data to the client whenever something changes, then you probably want to use a webSocket. 如果您希望能够在发生变化时将数据发送到客户端,那么您可能希望使用webSocket。 With a webSocket, the client makes a connection to the server and keeps the connection alive and then either side can send messages to the other. 使用webSocket,客户端建立与服务器的连接并保持连接活动,然后任何一方都可以向另一方发送消息。 In your particular instance, anytime something changes in the database, you could deliver that change to each connected client. 在您的特定实例中,只要数据库中发生更改,您就可以将更改传递给每个连接的客户端。 When the client Javascript receives this change, it could then update the currently displayed page. 当客户端Javascript收到此更改时,它可以更新当前显示的页面。

A common library for making webSockets easier is the socket.io library which works well with node.js. 用于使webSockets更容易的公共库是socket.io库,它与node.js配合得很好。 In this case, you would have on place in your code (not in a request handler) that listens for database changes and then when one happens, you send that to each connected webSocket. 在这种情况下,您将在代码中(而不是在请求处理程序中)监听数据库更改,然后在发生更改时,将其发送到每个连接的webSocket。

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

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