1

I'm using a combination of Express and Request (installed using npm) to try to send a get request to get some json from the server. However no matter what I do the body that is returned is "undefined".

This is the code in my server.js file. The json isn't actually what I'm sending, it's just an example as I can't post what I'm actually sending.

import express = require("express");
import bodyParser = require("body-parser");
let app = express();
app.use(bodyParser.json());

app.get('/config', function(req, res){
    res.json('{name: test}');
})

app.listen(3000);

I've tried both of the following but both of them say that body is undefined.

import request = require("request");

let req = {
    url: `http://localhost:3000/config`,
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
}

request(req, function(error, response, body){
    this.config = JSON.parse(body);
})

request(`/config`, function(err, res, body) {
    this.config = JSON.parse(body);
});

Does anyone know what I'm doing wrong? I've never used express or request before so any tips would be greatly appreciated.


UPDATE

If I change the request code to the following, the inside of the function is never run. Does anyone know why this would be?

let req = {
    url: `http://localhost:3000/config`,
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
}

request(req, function(error, response, body){
    console.log("response => "+JSON.parse(body));
    return JSON.parse(body);
})
annedroiid
  • 6,267
  • 11
  • 31
  • 54
  • I copy pasted your 1st example code and I got back '{name: test}' using `postman`. Is that the output you are expecting? – Samuel Toh Jul 21 '16 at 02:19
  • Yes that's what I want. I guess it must be something wrong with my Request stuff then. – annedroiid Jul 21 '16 at 02:44
  • Not sure about it. I was just doing a simple "GET" . Empty header. Maybe worth having a look at the tools or API that you are using to make the call, maybe it is adding some pepper and salt to the raw response. I would also check the HTTP response code and make sure it is not getting 404. – Samuel Toh Jul 21 '16 at 03:35
  • In the client code - your url should be `localhost:3000/config`. – Samuel Toh Jul 21 '16 at 03:39
  • When I do it in postman it says "Cannot GET/config". How did you get the response back? – annedroiid Jul 21 '16 at 04:02

2 Answers2

2

Since OP hasn't got it working and I believe the code he got up there is correct. I may as well post my working solution here to help him get started.

Hopefully this will save you hours of debugging...

Client:

"use strict";
let request = require("request");

let req = {
    url: `localhost:4444/config`,
    proxy: 'http://localhost:4444',
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
};

request(req, function (err, res, body) {
    this.config = JSON.parse(body);
    console.log("response => " + this.config);
});

Server:

"use strict";
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var config = require('config');
app.use(bodyParser.json());

app.get('/config', function(req, res){
    res.json('{name: test}');
});

// Start the server
app.set('port', 4444);

app.listen(app.get('port'), "0.0.0.0", function() {
    console.log('started');
});

Output:

response => {name: test}

Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
1

I dont't know if you have posted whole of your server's code, it seems like you missed app.listen(port) so that your server cannot be started up correctly.

Also, if you added if (error) { console.log(error); } at the first line of the callback function of request, you'll find it print an error: [Error: Invalid URI "/config"]

And that's why the body is always undefined: You have to give the full url such like http://localhost:xxxx to request.

In short:

  • Your server didn't listen to a specific port. app.listen(5678)
  • Your client didn't know the complete url. request('http://localhost:5678/config', (...)=>{...})
Narcotics
  • 313
  • 1
  • 8
  • It's set to automatically use localhost. I also do have app.listen(3000) to the port, I just forgot to include it my comment. – annedroiid Jul 21 '16 at 03:36
  • automatically use localhost? Do you mean your client request? It won't, imho. And you didn't tell the client which port should it send the request to. Just print a log about the error, and you'll get the reason why it didn't work. Error message could always be very useful. – Narcotics Jul 21 '16 at 04:02