使用URL中定义的查询参数从带有node/express的RESTful API中筛选资源



我如何在node/express 上做这种事情

app.get("/users/:id/state/:state", (req, res) => {
if (req.params.state== 'Published') {
//do somehting
} else {
//do something
}
});

但按州过滤?Exampe,我想要这种类型的路线/users/123/posts?state=published,我必须如何在节点上纠缠它?

在express中,URL查询字符串不需要在路由中指定。相反,您可以使用req.query:访问它们

app.get("/users/:id/posts", (req, res) => {
if (req.query.state  == 'published') {
console.log("published");
} else {
console.log("not published");
}
});

这将处理url:/users/123/posts?state=published

最新更新