首页 > 解决方案 > 如何在 node js 中为 route.get 设置标头和参数?

问题描述

如何在 node js 中为 route.get 设置标头和参数?我想将标头值和参数设置为 API 数据调用 URL。

router.get("/getdata", async (req, res) => {
  res.header({
    'key': '123456'
  });
  await fetch(`https://example.com/api?param=${data}`)
    .then((data) => data.json())
    .then((data) => res.json({ msg: data }))
    .catch((err) => console.log(err));
});

我的问题是如何在 node js 中的 router.get 中设置标头和参数数据?

标签: javascriptnode.jsexpress

解决方案


阅读有关路由的 Express文档。它解释了如何使用例如req.query访问该param获取 URL 中的内容。


推荐阅读