无法在localhost中使用ExpressJS API使用查询参数
在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您的
index.js
和doutes.js
文件很好,现在您只需要正确发送请求,请参阅req.params不同,并且req.query是不同的第一个方法 在Postman中使用(查询)
:
您的索引。
在路由器文件中。
您是如何获得该ID的?
使用(params)的第二种方式 - 仔细查看Postman URL,然后
在Postman:
您的索引中的路线上查看。
在路由器文件中。
您是如何获得该ID的?
这是您可以获得ID的双向,如果您还有其他问题,请告诉我,我会尽力更加清晰地澄清您的观点。
Firstly your
index.js
androutes.js
file is fine now you just need to send request correctly see the req.params is different and the req.query is differentFirst Way with (Query)
In postman:
Your Index.
In Router file.
How did you get that id;
Second Way with (Params) - Look at the postman URL carefully and at Route
In postman:
Your Index.
In Router file.
How did you get that 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.
您没有在邮递员内正确调用API。您应该这样称呼:
You are not calling API correctly inside the Postman. You should call it like this:
在路由器文件中,您使用的是 rout有效的URL将为
http:// localhost:3000/api/v1/users/120622
。对于您的Postman API工作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