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
      }
    });
    
    评论

报告相同问题?