击打单个端点(nodejs)时处理不同的URL路径

发布于 2025-01-24 01:36:59 字数 1782 浏览 1 评论 0原文

我正在使用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.

enter image description here

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

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

发布评论

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

评论(1

梦亿 2025-01-31 01:36:59

使用路由参数

例如,将您的参数添加到路由器中,例如/:myParam,然后检查并运行您的代码:

numberRouter.post("/:myparam", async (req:Request, res:Response) => {
    const myParam = req.params.myparam;

    //what I want to do
    if (myParam == "one") {
        //do something
    } 
    else if (myParam == "two") {
       //do something
    }

});

use route parameters

add your parameter to the router, for example /:myparam, and then check it and run your code:

numberRouter.post("/:myparam", async (req:Request, res:Response) => {
    const myParam = req.params.myparam;

    //what I want to do
    if (myParam == "one") {
        //do something
    } 
    else if (myParam == "two") {
       //do something
    }

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