如何将用户输入从节点服务器传递到外部 api 调用?
Posted
技术标签:
【中文标题】如何将用户输入从节点服务器传递到外部 api 调用?【英文标题】:How do I pass user input from a node server to an external api call? 【发布时间】:2020-02-17 09:54:37 【问题描述】:我正在尝试 1) 从 React 表单中获取用户输入 2) 将其传递给 node.js 服务器 3) 然后传递给外部 API 搜索结果。 1 和 2 工作正常,但我无法弄清楚 #3。如何构造我的代码,以便我可以将用户输入/发布请求的结果传递给这个调用?
return spotifyApi.getArtistTopTracks('user input', 'US');
server.js
const server = express();
const router = express.Router();
const cors = require('cors');
const bodyParser = require('body-parser');
const logger = require('morgan');
const helmet = require('helmet');
const SpotifyWebApi = require('spotify-web-api-node');
const port = process.env.PORT || 5000;
server.use(logger('dev'));
server.use(cors());
server.use(bodyParser.urlencoded( extended: false ));
server.use(bodyParser.json());
server.use(helmet())
server.listen(port, () => console.log(`Listening on port $port`))
module.exports = server;
var urlencodedParser = bodyParser.urlencoded( extended: false )
server.get('/search', function (req, res)
res.send(query: req.query)
)
server.post('/search', urlencodedParser, function (req, res)
console.log(req.body)
res.send(query: req.query)
)
let credentials =
clientId: 'clientId',
clientSecret: 'clientSecret',
redirectUri: 'http://localhost:5000/callback'
let spotifyApi = new SpotifyWebApi(credentials);
spotifyApi.clientCredentialsGrant()
.then(function(data)
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
spotifyApi.setAccessToken(data.body['access_token']);
return spotifyApi.getArtistTopTracks('4TMHGUX5WI7OOm53PqSDAT', 'US');
)
.then(function(data)
server.get('/tracks', (req, res) =>
res.send(data.body);
)
console.log('The most popular tracks are:');
let ids = data.body.tracks.map(track => track.id);
console.log(ids)
return spotifyApi.getAudioFeaturesForTracks(ids)
.then(function(data)
server.get('/tracks/audio_features', (req, res) =>
res.send(data.body);
)
console.log(data.body);
, function(err)
done(err);
)
),
function(err)
console.log('Something went wrong!', err);
```
【问题讨论】:
你想从 spotifyApi.getArtistTopTracks api 获取曲目并在你的 server.post('/search' 路由中返回响应吗? @SuleymanSah 我想接受用户输入,传递给 spotifyApi.getArtistTopTracks api 并返回响应。 在这里? server.post('/search' 路由,你的搜索词的查询参数的名称是什么? 查询:req.query 请问一下?像这样结束: /?id=1234 ? 【参考方案1】:你能试试这个例子并说出结果吗?
server.post("/search", urlencodedParser, async function(req, res)
console.log("body: ", req.body);
console.log("query: ", req.query);
try
let credentials =
clientId: "clientId",
clientSecret: "clientSecret",
redirectUri: "http://localhost:5000/callback"
;
let spotifyApi = new SpotifyWebApi(credentials);
const grandtObject = await spotifyApi.clientCredentialsGrant();
spotifyApi.setAccessToken(grandtObject.body["access_token"]);
const response = await spotifyApi.getArtistTopTracks(
"4TMHGUX5WI7OOm53PqSDAT", // can be changed according to the req.query for example req.query.id
"US"
);
console.log(response);
res.send( tracks: response.body );
catch (err)
console.log(err);
res.status(500).send("something went bad");
);
【讨论】:
这并没有解决它,但你给我指出了正确的方向。我会尽快发布解决方案。谢谢! @codermama 欢迎您,您至少可以尝试为答案投票以上是关于如何将用户输入从节点服务器传递到外部 api 调用?的主要内容,如果未能解决你的问题,请参考以下文章
在节点中创建 REST API 时,如何将来自对外部网站的请求的 http 响应流式传输到原始 api 调用?
如何将参数从 Angular 2 服务传递到节点 js 函数?意思是
如何将 Windows 身份验证凭据从客户端传递到 Web API 服务