击打单个端点(nodejs)时处理不同的URL路径
我正在使用Express Nodejs登录端点:
app.use("/number", numberRouter);
现在我必须处理路径不同的URL。在这里,我们在同一URL中有三个不同的路径:
- https:// localhost:8080/number/一个
- https:// localhost:8080/number/两个
- https:// https:// localhost:8080/number/
thr处理:
numberRouter.post("/", async (req:Request, res:Response) => {
var url = req.protocol + '://' + req.get('host') + req.originalUrl;
//what I want to do
if (req.path == "one") {
//do something
}
else if (req.path == "two") {
//do something
}
});
我要实现的是,一旦我击中number
端点,我获取了完整的URL,将其提取为path
,并基于path < /code>我做进一步的处理,而不是击中三个不同的端点(
/number/一个
,/number/two
,/number/number/三
) 。 这可能吗?
我正在使用Postman进行测试,如果我发送带有以下URL的邮政请求: localhost:8080/number/One
发布请求失败。我希望在代码中进行这样的内容:
numberRouter.post("/variablePath", async (req:Request, res:Response) => { ... }
其中variable PathE
是通过Postman设置的(一个
,两个
或三个
)然后在这里处理。
解决方案(遵循 @traynor的答案):
app.use("/number", numberRouter);
numberRouter.post("/:pathNum", async (req:Request, res:Response) => {
if (req.path === "/one") {
//do something
}
else if (req.path === "/two") {
//do something
}
});
通过Postman的发布请求:localhost:8080/number/:pathnum
。 在Postman中,在params
部分中的pathnum
的值 path变量标题。
I am using express nodejs to hit an endpoint:
app.use("/number", numberRouter);
Now I have to handle URL in which the path is different. Here we have three different paths in the same URL:
- https://localhost:8080/number/one
- https://localhost:8080/number/two
- https://localhost:8080/number/three
The file in which the numberRouter is handled:
numberRouter.post("/", async (req:Request, res:Response) => {
var url = req.protocol + '://' + req.get('host') + req.originalUrl;
//what I want to do
if (req.path == "one") {
//do something
}
else if (req.path == "two") {
//do something
}
});
What I want to achieve is that once I hit the number
endpoint, I fetch the full URL, extract out it's path
and based on the path
I do further processing instead of hitting three different endpoints (/number/one
, /number/two
, /number/three
).
Is this possible?
I am using postman for testing and if I send a post request with the following URL:localhost:8080/number/one
the post request fails. I want something like this in the code:
numberRouter.post("/variablePath", async (req:Request, res:Response) => { ... }
where the variablePath
is set via postman (one
, two
or three
) and then processed here.
SOLUTION (following @traynor's answer):
app.use("/number", numberRouter);
numberRouter.post("/:pathNum", async (req:Request, res:Response) => {
if (req.path === "/one") {
//do something
}
else if (req.path === "/two") {
//do something
}
});
The post request via postman: localhost:8080/number/:pathNum
.
In postman set the value of pathNum
in the Params
section under the Path Variables
heading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用路由参数
例如,将您的参数添加到路由器中,例如
/:myParam
,然后检查并运行您的代码:use route parameters
add your parameter to the router, for example
/:myparam
, and then check it and run your code: