new to Node over here, I'm using postman to generate get requests and trying to get a json return which is currently only being displayed through console.log on my cmd. res.send
is returning an error that says res.send
is not a function.
require('dotenv').config();
const request = require('request');
const app = require('express');
module.exports = {
getToken: function(url, callback) {
const options = {
url: process.env.GET_TOKEN,
json: true,
body: {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'client_credentials'
}
};
request.post(options, (err, res, body) => {
if(err) {
return console.log(err)
}
console.log(`Status: ${res.statusCode}`)
console.log(body);
callback(res);
});
},
//var AT = '';
//getToken(process.env.GET_TOKEN, (res) => {
// AT = res.body.access_token;
// return AT;
// });
getGames: function (url, accessToken, callback) {
const gameOptions = {
url: process.env.GET_GAMES,
method: 'GET',
headers: {
'Client-ID': process.env.CLIENT_ID,
'Authorization': 'Bearer ' accessToken
}
};
request.get(gameOptions, (err, res, body) => {
if(err) {
return console.log(err);
}
console.log(`Status: ${res.statusCode}`);
console.log(JSON.parse(body));
res.send(JSON.parse(body));
});
}
// setTimeout(() => {
// getGames(process.env.GET_GAMES, AT, (response) => {
// });
// }, 1000);
}
The code below is my main module:
const express = require('express');
const app = express();
const PORT = 5000;
const apicall = require('./apicall');
app.get('/', (req, res) => {
res.send("Hello world!")
});
app.get('/ne', (req, res) => {
res.send('This is the new endpoint');
});
app.get('/getinfo', (req, res, body) => {
res.send(apicall);
//res.send(body);
//console.log(body);
var AT = '';
apicall.getToken(process.env.GET_TOKEN, (res) => {
AT = res.body.access_token;
return AT;
});
setTimeout(() => {
apicall.getGames(process.env.GET_GAMES, AT, (response) => {
});
}, 1000);
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});
CodePudding user response:
You use res.send
in the callback of a request.get
. But in that context, res
is the incoming response from the API that you call, not the outgoing response created by your app. Only the outgoing response contains a send
method.
To keep both separate, use different names:
app.get("/getinfo", function(req, res) {
request.get(..., function(err, incoming_res, body) {
res.json(JSON.parse(body));
});
});
CodePudding user response:
res.send
is a part of express. If the res.send that's failing is in request.get
then that's because it's not a part of express
.
From the docs for request
it says that the response argument will be an instance of http.IncomingMessage. That should mean you can simply use res.end
Edit:
@HeikoTheißen is right. There is no res.end
.
But this could be handled in a different way. If we can wrap the get request inside a promise, then we could resolve the promise with whatever needs to be sent from the get request.
An example:
const result = await new Promise((resolve) => {
request(gameOptions, function (error, response, body) {
resolve ({status : 'A Ok!'}) // <--- send response here
}
}
console.log ("result is ", result) // <-- Object {status : 'A Ok!'}