无法在localhost中使用ExpressJS API使用查询参数

发布于 2025-02-10 22:40:26 字数 480 浏览 1 评论 0原文

在我的index.js

app.use('/api/v1/users', userRouter)

文件中的文件中

router.get("/:id", getUserDataById);

postman中的

http:// localhost:3000/api/v1/users?id = 120622

它给出的错误:

Cannot GET /api/v1/users

我认为,这是根据我遵循的文档和教程给出查询参数的方式,但是此错误不会消失。

如果我删除查询,则其他端点可以很好地工作。

我无法在这里遇到问题。

从过去的两天开始,我一直坚持这样做。 只是解决这个问题的提示,将对我有很大帮助。

提前致谢!

In my index.js file

app.use('/api/v1/users', userRouter)

In Router file

router.get("/:id", getUserDataById);

In postman:

My GET URL looks like this: http://localhost:3000/api/v1/users?id=120622

Error it gives:

Cannot GET /api/v1/users

I think, this is how query param should be given according to the docs and tutorials i followed, but this error won't go away.

If i remove query, then other endpoints work perfectly.

I am not able to catch what's going wrong here.

I am stuck with this from last 2 days.
Just a hint to resolve this, will help me a lot.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没︽人懂的悲伤 2025-02-17 22:40:26

首先,您的index.jsdoutes.js文件很好,现在您只需要正确发送请求,请参阅req.params不同,并且req.query是不同的

第一个方法 在Postman中使用(查询)

http://localhost:3000/api/v1/users?id=120622

您的索引。

app.use('/api/v1/users', userRouter);

在路由器文件中。

router.get("/", getUserDataById);

您是如何获得该ID的?

let id = req.query.id;

使用(params)的第二种方式 - 仔细查看Postman URL,然后

在Postman:

http://localhost:3000/api/v1/users/120622

您的索引中的路线上查看。

app.use('/api/v1/users', userRouter);

在路由器文件中。

router.get("/:id", getUserDataById);

您是如何获得该ID的?

let id = req.params.id;

这是您可以获得ID的双向,如果您还有其他问题,请告诉我,我会尽力更加清晰地澄清您的观点。

Firstly your index.js and routes.js file is fine now you just need to send request correctly see the req.params is different and the req.query is different

First Way with (Query)

In postman:

http://localhost:3000/api/v1/users?id=120622

Your Index.

app.use('/api/v1/users', userRouter);

In Router file.

router.get("/", getUserDataById);

How did you get that id;

let id = req.query.id;

Second Way with (Params) - Look at the postman URL carefully and at Route

In postman:

http://localhost:3000/api/v1/users/120622

Your Index.

app.use('/api/v1/users', userRouter);

In Router file.

router.get("/:id", getUserDataById);

How did you get that id;

let id = req.params.id;

This is the two-way you can get your id, Let me know if you have any more questions and I'll try to clarify your points with more clarity.

寄居人 2025-02-17 22:40:26

您没有在邮递员内正确调用API。您应该这样称呼:

http://localhost:3000/api/v1/users/120622

You are not calling API correctly inside the Postman. You should call it like this:

http://localhost:3000/api/v1/users/120622
ζ澈沫 2025-02-17 22:40:26

In the router file, what you are using is Route Parameter where the valid URL would be http://localhost:3000/api/v1/users/120622. for your Postman api to work you should remove :id at router file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文