繁体   English   中英

如何使用 node.js 在重定向 url 中获取元素

[英]how can I get an element in a redirect url using node.js

非英语国家,请原谅我的拼写错误。

例如,我想首先将 url1( http://localhost:3000/api/song/167278 ) 重定向到 url2( http://localhost:4000/api/song/167278 ) 以使用 url2 的 api。 并且url2会响应一个json文件,可以在postman的面板中看到。

邮递员面板

但是可能有很多元素,我只想要文件中的一个元素,例如data[0].url 当人们访问http://localhost:3000/api/song/167278时,如何仅返回url 值(此 json 中的data[0].url )。

我现在正在使用 express.js,我该如何编辑它? 或者还有其他方法吗?

app.get('api/song/:id', async (req, res) => {
  try {
    const { id } = req.params
    url = "http://localhost:4000/api/song/" + id
    res.redirect(url)
  }
  catch (e) {
    console.log(e)
  } 
}

您可以在那里代理整个请求,也可以在您的请求处理程序中获取localhost:4000/api/song/1 (使用 node-fetch 或 axios 或节点的 API 之类的东西,并将您想要的字段作为 json 发送回客户端。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM