suchen

Heim  >  Fragen und Antworten  >  Hauptteil

node.js - 如何用nodejs写一个接口,随便写一个就好

在个人主页这里面,新手求教

高洛峰高洛峰2788 Tage vor424

Antworte allen(3)Ich werde antworten

  • ringa_lee

    ringa_lee2017-04-17 16:27:31

    去看官网文档吧,用 express 或者 koa

    Antwort
    0
  • ringa_lee

    ringa_lee2017-04-17 16:27:31

    router.post("/changePwd", (req, res, next) => {
        var oldPwd = req.body.oldPwd,
            newPwd = req.body.newPwd,
            uid = req.session.user.id;
    
        var ep = new EventProxy();
    
        ep.fail(err => {
            logger.error("~update user password failed .", err);
            res.json({status: err.status || se.code.failed});
        });
    
        if (!oldPwd || !newPwd) {
            return ep.throw(se.error(se.code.paramIllegal));
        }
    
        if(oldPwd == newPwd){
            return ep.throw(se.error(se.code.pwdSame));
        }
    
        admin.findUserById(uid, ep.done(user => {
            if(user[0].password != encrypt.encrypt(oldPwd)){
                return ep.throw(se.error(se.code.verifyFailed));
            }
    
            if(user[0].password == encrypt.encrypt(newPwd)){
                return ep.throw(se.error(se.code.pwdSame));
            }
    
            admin.updatePassword(encrypt.encrypt(newPwd),uid,ep.done(count => {
                if(count > 0){
                    res.json({status : se.code.success});
                }else{
                    res.json({status : se.code.failed});
                }
            }));
        }));
    
    
    });

    Antwort
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:27:31

    返回个json格式的字符串即可

    Antwort
    0
  • StornierenAntwort