我正在使用 Angular 和 nodeJS 开发一个简单的网站。在访问节点服务器中定义的路由时,我试图从 Angular 服务发送一个参数“电子邮件”。Angular 服务功能:
节点服务器的配置:
我尝试使用 bodyParser 并使用 req.body 获取从前端发送的电子邮件参数,但它似乎丢失了。打印 req.params 或 req.body 时,它会得到一个空字典。

有人知道会发生什么吗?谢谢阅读!
从外观上看,您想param从请求 url 中读取路径(请参阅https://expressjs.com/en/guide/routing.html下的 route-params )。
在 nodejs 方面,您需要将处理程序更改为:
router.get('/profile/:email', ...) => {
const email = req.params.email;
});
在客户端,您需要正确添加电子邮件路径段。一种方法是:
this.http.get<any>(`${this.URL}/profile/` + encodeURIComponent(email));
path您也可以使用-parameters 来代替query-parameters。您需要进行以下更改:
节点:
router.get('/profile', ...) => {
const email = req.query.email;
});
角度:
this.http.get<any>(`${this.URL}/profile`, { params: { email } });