如何使用 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's pannel)
但是可能有很多元素,我只想要文件中的一个元素,比如data[0].url
。当人们访问 http://localhost:3000/api/song/167278
.
我现在正在使用 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)
}
}
您可以 proxy the entire request there or fetch localhost:4000/api/song/1
in your request handler (with something like node-fetch or axios or with node's APIs 并将您想要的字段作为 json 发送回客户端。
非英语国家,请原谅我的拼写错误。
例如,我想先将url1(http://localhost:3000/api/song/167278
)重定向到url2(http://localhost:4000/api/song/167278
)来使用url2的api。而url2会回复一个json文件,可以在邮递员面板看到。
(postman's pannel)
但是可能有很多元素,我只想要文件中的一个元素,比如data[0].url
。当人们访问 http://localhost:3000/api/song/167278
.
我现在正在使用 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)
}
}
您可以 proxy the entire request there or fetch localhost:4000/api/song/1
in your request handler (with something like node-fetch or axios or with node's APIs 并将您想要的字段作为 json 发送回客户端。