如何修改我在node.js中提供该页面 [英] How do I modify the page I'm serving in node.js

查看:160
本文介绍了如何修改我在node.js中提供该页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我有我的server.js

Ok, so I have my server.js

var express     = require("express"),
    app         = express(),
    bodyParser  = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(express.static(__dirname + '/'));

app.post('/message', function(req, res) {
  var jsonData = req.body;
  if (jsonData.hasOwnProperty('phone1')) {
      console.log("Phone 1 is connected to", jsonData.phone1.connection,
                  "and has a downlink speed of", jsonData.phone1.dl, "Mbps");
  } else 
  if (jsonData.hasOwnProperty('phone2')) {
      console.log("Phone 2 is connected to", jsonData.phone2.connection,
                  "and has a downlink speed of", jsonData.phone2.dl, "Mbps");
  }
});

var port = 1337;
app.listen(port);
console.log("Running at Port " + port);

和你看我要当服务器获得的东西贴在/消息做的东西。我可以CONSOLE.LOG东西,是的,但我想改变的东西的网页此服务器上的服务的POST请求从另一台服务器来。该服务器只presents他们。

and as you see I want to do stuff when the server gets something posted on /message. I can console.log stuff yes, but I want to change things on the web page this server is serving. The POST requests are coming from another server. This server only presents them.

我怎么能做到这一点,而不必更新页面?

How can I do that without having to update the page?

我还使用AngularJS在客户端,因此任何方式为客户端拿起JSON数据将是很好的。

I'm also using AngularJS on the client side, so any way for the client side to pick up the JSON data would be nice.

我希望present数据在我的Highcharts仪表和图表,但页面元素的一个简单的文本更新(如< p ID =榜样> )将一个回答这个问题做就好了。

I hope to present the data in my Highcharts gauges and charts but a simple text update on a page element (e.g. <p id="example">) will do just fine for an answer to this question.

我知道我能得到jQuery来节点,但我仍然缺乏窗口元素操纵presented数据。 <一href=\"https://github.com/nwjs/nw.js/wiki/Transfer-objects-between-window-and-node#bridge-between-window-and-node\"相对=nofollow> NW.js 可能会做正是我想要的,我还没有尝试过还是不过它,但我怀疑有可能是另一种解决这个问题。

I know I can get jquery to node but I still lack the window element to manipulate the presented data. NW.js might do exactly what I want, I haven't still tried it though, but I suspect there might be another solution to this problem.

推荐答案

由于我只需要从服务器向客户端发送的数据,我结束了与服务器发送事件(SSE)和我的code看起来像这样在服务器端:

Since I only need to send data from the server to the client, I ended up with Server Sent Events (SSE) and my code looks like this on the server side:

var mypage;

app.get('/connect', function(req, res){
    res.writeHead(200, {
      'Connection': 'keep-alive',
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache'
    });
    mypage = res;
});

app.post('/message', function(req, res) {
    if (mypage !== undefined) {
        mypage.write('data: '+ JSON.stringify( req.body ) +'\n\n');
    }
});

和在客户端

$(function () {
    var server = new EventSource('/connect'),
        point1 = $("#gauge1").highcharts().series[0].points[0],
        point2 = $("#gauge2").highcharts().series[0].points[0];
    server.onmessage = function(e) {
        var jsonData = JSON.parse(e.data);
        if (jsonData.hasOwnProperty('phone1')) {
            point1.update(parseInt(jsonData.phone1.dl));
            if (jsonData.phone1.connection === "3G") {
                /* extra functionality */
            } else {
                /* extra functionality */
            }
        } else 
        if (jsonData.hasOwnProperty('phone2')) {
            point2.update(parseInt(jsonData.phone2.dl));
            if (jsonData.phone2.connection === "3G") {
                /* extra functionality */
            } else {
                /* extra functionality */
            }
        }
    };
});

我得到了额外的帮助,<一个href=\"http://stackoverflow.com/questions/31700336/server-sent-event-eventsource-with-node-js-ex$p$pss\">my上证所有关的数据格式等问题

节点的Webkit没有回答我的问题,因为它不会在浏览器中运行。

Node Webkit was not the answer to my problem, since it doesn't run on the browser.

我仍然有一些问题,收到了许多文章。我生成每0.5秒后的消息,但我不断收到5的消息则事2分钟然后再5的消息。我不知道为什么会发生的事情,但它不是这个问题的一部分,所以我会发布有关的另一个问题。

I still have some problems with receiving many posts. I'm generating every 0.5s a post message but I keep receiving 5 messages then nothing for 2 minutes then again 5 messages. I don't know why that's happening, but it's not part of this problem so I'll post another question about that.

这篇关于如何修改我在node.js中提供该页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆