Sending/handling GET requests in typescript using Express, Request and Node.js
<p>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". </p>

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

<pre><code>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);
</code></pre>

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

<pre><code>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);
});
</code></pre>

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

<hr>

<p><strong>UPDATE</strong></p>

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

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

request(req, function(error, response, body){
    console.log("response =&gt; "+JSON.parse(body));
    return JSON.parse(body);
})
</code></pre>