简体   繁体   English

路由快递节点js

[英]routing express node js

I have the following routes methods in my controller: 我的控制器中有以下route方法:

getListaUsuarios() {
    this.app.get("/api/usuario", (req, res) => {
        this.usuario.getListaUsuarios().then((results) => {
            return res.status(200).json(results);
        }).catch((error) => {
            return res.status(500).send(error);
        });
    });
}

getUsuarioByEmail() {
    this.app.get("/api/usuario/:usuarioEmail", (req, res) => {
        let usuarioEmail = req.params.usuarioEmail;
        this.usuario.getUsuarioByEmail(usuarioEmail).then((results) => {
            if(!results) return res.status(404).json();
            return res.status(200).json(results);
        }).catch((error) => {
            return res.status(500).send(error);
        });
    });
}

My question is related with the routes, for best practices we know that we should be using the name of the resources, in the first method I'm retrieving the list of Users and in the second I'm trying to retrieve the user by email. 我的问题与路线有关,对于最佳实践,我们知道我们应该使用资源的名称,第一种方法是检索用户列表,第二种方法是尝试通过电子邮件检索用户。 But when I try to call the api with: localhost:3000/api/usuario?usuarioEmail=xxxx@xxx.com it always call the first method and not the second. 但是,当我尝试使用以下命令调用api: localhost:3000/api/usuario?usuarioEmail=xxxx@xxx.com它总是调用第一种方法,而不是第二种。 Is there something wrong with the way I'm defining my routes, or I have to change the complete path always. 我定义路线的方式是否有问题,或者我必须始终更改完整路径。

In the url localhost:3000/api/usuario?usuarioEmail=xxxx@xxx.com , usuarioEmail is a query string; 在url localhost:3000/api/usuario?usuarioEmail=xxxx@xxx.comusuarioEmail是查询字符串; your rout is expecting it as a parameter. 您的溃败期望它作为参数。 The correct usage given your routes would be: 给定您的路线的正确用法是:

localhost:3000/api/usuario/xxxx%40xxx.com

Where %40 represents the URI encoding of @ . 其中%40表示@的URI编码。

If you actually wanted to use query strings, you would need to do something like this: 如果您确实想使用查询字符串,则需要执行以下操作:

getListaUsuarios() {
    this.app.get("/api/usuario", (req, res) => {
        if (!req.query.usarioEmail) { // check for the query string
            let usuarioEmail = req.params.usuarioEmail;
            this.usuario.getUsuarioByEmail(usuarioEmail).then((results) => {
                 if(!results) return res.status(404).json();
                 return res.status(200).json(results);
            }).catch((error) => {
                return res.status(500).send(error);
            });
        } else {
            this.usuario.getListaUsuarios().then((results) => {
                return res.status(200).json(results);
            }).catch((error) => {
                return res.status(500).send(error);
            });
        }
    });
}

Apparently you are mixing req.params with req.query 显然您正在将req.paramsreq.query混合

req.query

Works when you want to use an url just like the one you posted 当您想要使用与您发布的网址相同的网址时可以使用

localhost:3000/api/usuario?usuarioEmail=xxxx%40xxx.com

and the logic has to be setup in the /api/usuarios route 并且必须在/api/usuarios路由中设置逻辑

getListaUsuarios() {
    this.app.get("/api/usuario", (req, res) => {
      if (req.query.usuarioEmail) { //Aply your logic over here
        /*If wont change the value of usuarioEmail, you should use 
          const instead of let*/
        const usuarioEmail = req.params.usuarioEmail;
        return this.usuario.getUsuarioByEmail(usuarioEmail).then((results) => {
            if(!results) return res.status(404).json();
            return res.status(200).json(results);
        }).catch((error) => {
            return res.status(500).send(error);
        });        
      }
        this.usuario.getListaUsuarios().then((results) => {
            return res.status(200).json(results);
        }).catch((error) => {
            return res.status(500).send(error);
        });
    });
}

req.params

Is used when you want to do something like this 当您想做这样的事情时使用

localhost:3000/api/usuario/micorreo%40email.com

And your route will look exactly like you have defined it 您的路线将看起来像您已定义的路线

/api/usuario/:usuarioEmail

The value of :usuarioEmail is in req.params.usuarioEmail :usuarioEmail的值在req.params.usuarioEmail

If you want to know more about the req object, here's a link to the Express documentation 如果您想了解更多有关req对象的信息,请访问Express文档的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM