[英]How can I show JSON response in Postman, GET request Express?
[英]How can I fix my code to make it send the json data as a response to postman's GET requests?
我通过代码接收的数据通过console.log
输出到 cmd,但我似乎无法弄清楚如何使相同的数据可用于邮递员的GET
请求。 谢谢
const express = require('express');
const app = express();
const PORT = 5000;
const apicall = require('./apicall');
const request = require('request');
app.get('/', (req, res) => {
res.send("Hello world!")
});
app.get('/getinfo', (req, res, body) => {
const getToken = (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 = '';
var info = '';
getToken(process.env.GET_TOKEN, (res) => {
AT = res.body.access_token;
return AT;
});
const getGames = (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);
}
let x = '';
console.log(`Status: ${res.statusCode}`);
console.log(JSON.parse(body));
//res.send(parsed);
//req.body.getinfo = JSON.parse(body);
})
}
setTimeout(() => {
getGames(process.env.GET_GAMES, AT, (response) => {
});
}, 1000);
//res.send(JSON.parse(body));
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});
您在request.get
的回调中使用res.send
。 但在这种情况下, res
是来自您调用的 API 的传入响应,而不是您的应用程序创建的传出响应。 只有传出响应包含send
方法。
为了使两者分开,请使用不同的名称:
app.get("/getinfo", function(req, res) {
request.get(..., function(err, incoming_res, body) {
res.json(JSON.parse(body));
});
});
res.send
是 express 的一部分。 如果失败的 res.send 在request.get
,那是因为它不是express
的一部分。
从request
的文档中,它说响应参数将是 http.IncomingMessage 的一个实例。 这应该意味着您可以简单地使用res.end
您只需将其通过管道传递给响应.pipe(res)
const express = require('express');
const app = express();
const PORT = 5000;
const apicall = require('./apicall');
const request = require('request');
app.get('/', (req, res) => {
res.send("Hello world!")
});
app.get('/ne2', (req, res) => {
//res.send('This is the new endpoint');
apicall.getCall;
});
app.get('/getinfo', (req, res, body) => {
const getToken = (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 = '';
var info = '';
getToken(process.env.GET_TOKEN, (res) => {
AT = res.body.access_token;
return AT;
});
const getGames = (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);
}
let x = '';
console.log(`Status: ${res.statusCode}`);
//console.log(JSON.parse(body));
info = JSON.parse(body);
console.log(info);
//res.send(parsed);
//req.body.getinfo = JSON.parse(body);
}).pipe(res);
}
setTimeout(() => {
getGames(process.env.GET_GAMES, AT, (response) => {
});
}, 1000);
//res.send(info);
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.