Giter Site home page Giter Site logo

cloud-server's Introduction

项目地址为: api

这个是一个RESTful API例子,登录认证采用的是JSON Web Token (JWT),接口文档采用的是apidoc。

npm install
npm start
//生成接口文档
apidoc -i ./routes/  -o ./public/apidoc

1.登录授权返回token,设置有效期

//登录验证成功后,返回给前端
    var authToken = jwt.sign({username: user.username,exp:parseInt(Date.now()/1000)+(60)}, config.session.secrets);
     res.json({success:1,msg:"登录成功",token: authToken});

2.设置目录v1下面采用JWT验证,对每次访问进行解密排除url是login和signup

router.use(function(req, res, next) {
    if (req.originalUrl.indexOf("/login") < 0&&req.originalUrl.indexOf("/signup")<0) {
    // 拿取token 数据 按照自己传递方式写
    var token = req.body.token || req.query.token || req.headers['x-access-token'];
    if (token) {
        // 解码 token (验证 secret 和检查有效期(exp))
        jwt.verify(token, config.session.secrets, function(err, decoded) {
              if (err) {
            return res.json({ success: false, message: '无效的token.' });
              } else {
                // 如果验证通过,在req中写入解密结果
                req.decoded = decoded;
                next(); //继续下一步路由
          }
        });
      } else {
        // 没有拿到token 返回错误
        return res.status(403).send({
            success: false,
            message: '没有找到token.'
        });
      }
    }else{
        next();
    }
    });

3.设置api注释自动生成文档,具体参考apidoc

这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述

cloud-server's People

Contributors

harryluo163 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.