2

I am using express.js and I need to make a call to HTTP GET request ,to fetch JSON data .Please suggest me some good node js/express js modules/lib to perform get/post request .

2 Answers 2

2

Node.js provides an extremely simple API for this functionality in the form of http.request.

var http = require('http');

//The url we want is: 'www.random.com/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
  host: 'www.random.com',
  path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};

callback = function(response) {
  var str = '';

  //another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });

  //the whole response has been recieved, so we just print it out here
  response.on('end', function () {
    console.log(str);
  });
}

http.request(options, callback).end();

Here I attach some more examples with POST and custom headers. If you don't need special things, I'd stick to the native code.

Besides, Request, Superagent or Requestify are pretty good libraries to use.

Sign up to request clarification or add additional context in comments.

Comments

2
var express = require('express');
var app = express();
var fs = require('fs');

app.get('/', function (req, res) {
    fs.readFile('./test.json', 'utf8', function (err, data) {
        if (err) {
            res.send({error: err});
        }
        res.send(data);
    })
});
var server = app.listen(3001, function () {
    console.log('Example app listening port 3001');
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.