weixin_33728268 2014-08-26 16:42 采纳率: 0%
浏览 134

NodeJS服务器请求

I'm working with NodeJS and I'm still familiarizing with it.

Given the structure of my system I have two NodeJS servers running in different machines. The user/browser sends a request to the first server, which returns to the browser a JSON file that is located in this first machine.

This first server also updates this JSON file every 5-10 seconds sending a request to the second server, which returns another JSON file which data will overwrite the one in the JSON file in the first server, so the next user/browser request will be updated. This second server also has a NodeJS server running but it only dispatches the request coming from the first server.

I have this structure since I don't want the user to know about the second server for security reasons (Anyone could see the redirection with any dev tools).

This two events are executed asynchronously since the Browser petitions may be in different time from the event updating the JSON file.

My question is: How can I update the JSON file in the first server? I wonder if there's a NodeJS library I can use for requesting the new JSON file to the second server.

I make the Browser-FirstServer petition via AJAX and everything works properly, but AJAX only works on the client side, so I'm not really sure how to do this for the First-Second server request.

Any help would be appreciated.

Something i'm xpecting for is the following:

setInterval(function(){ 
// make request to server 2

//receive JSON file

// use 'fs' for overwriting the JSON from server 1

}, 5000)
  • 写回答

2条回答 默认 最新

  • weixin_33735077 2014-08-26 17:12
    关注

    You can either use the built in http/https modules in nodejs or use something like request

    var request = require('request');
    request('/url/for/json', function (error, response, body) {
      if (!error && response.statusCode == 200) {
        //write body to the file system
      }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站