问题描述:

So bear with me because I feel that I've tried everything there is to try. I'm really new to JavaScript and I've primarily worked with C/C++ before and I am still a novice with respect to those languages as well. I tried to create a page on my aws services (ec2) server that when accessed with a get request of a URL it would use my server-side API key and return JSON data that it requested from the URL given. I was working with the League of Legends API and with NodeJs using forever start *.js, handlebars and express JavaScript as well.

I feel that it is important to mention that while this issue arose doing an assignment for a class I have since completed the assignment without including that functionality.

I tried to do this as both synchronous and asynchronous and it didn't work for me. I feel that it is because using var foo = new XMLHttpRequest is not the correct way to send a get request from a server to another server. When I tried to find a better way to do this using nodejs I could not find anything which made sense to me or that I could understand.

Please let me know why this is wrong/ why this breaks and how I can make it not break or do it properly. I couldn't find any similar stack overflow questions despite a few hours of struggling and searching.

app.get('/test',function(req,res){

//if(url == undefined){

var url = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/RiotSchmick";

//}

var KEY= "REDACTED_FOR_POST";//note I have a functional api

//key that I can correctly use to view data through a browser get request.

var local = new XMLHttpRequest();//this line prevents this page from runnning

local.open("GET", "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/RiotSchmick?api_key=REDACTED_FOR_POST", false);

//RiotSchmick is used as an example "summoner" name in quick start

local.send(null);

var content = {};//holder for handlebars

content.text = local.responseText;//I expected this to have a string form JSON.

res.render('buffer.handlebars', content);

//buffer.handlebars is one line: {{text}} and properly prints data when

//content.text is assigned to a constant such as "hello"

})

Thanks for help and your time.

网友答案:

Unless you are using something like https://github.com/driverdan/node-XMLHttpRequest there's no XMLHttpRequest in node. XMLHttpRequest is an object that only exists in browsers.

You have to require http to make http requests in node.

var http = require('http');

Then you can do something like that (taken from the docs):

var postData = querystring.stringify({
  'msg' : 'Hello World!'
});

var options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
};

var req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.')
  })
});

req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});

// write data to request body
req.write(postData);
req.end(

);

相关阅读:
Top