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);
})
5
  • I copy pasted your 1st example code and I got back '{name: test}' using postman. Is that the output you are expecting? Commented Jul 21, 2016 at 2:19
  • Yes that's what I want. I guess it must be something wrong with my Request stuff then. Commented Jul 21, 2016 at 2: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. Commented Jul 21, 2016 at 3:35
  • In the client code - your url should be localhost:3000/config. Commented Jul 21, 2016 at 3:39
  • When I do it in postman it says "Cannot GET/config". How did you get the response back? Commented Jul 21, 2016 at 4:02

2 Answers 2

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}

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

3 Comments

@annedroiid Let me know if this works for you. Note I'm using a different port here. 4444
When I put that in I get "Error: Invalid protocol: localhost:"
@annedroiid cant replicate it... I will remove that proxy line and see what happens...
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', (...)=>{...})

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.