1.0.5 • Published 1 month ago

koa-dlzc v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

gitter NPM version build status Test coverage OpenCollective Backers OpenCollective Sponsors PR's Welcome

Expressive HTTP middleware framework for node.js to make web applications and APIs more enjoyable to write. Koa's middleware stack flows in a stack-like manner, allowing you to perform actions downstream then filter and manipulate the response upstream.

Only methods that are common to nearly all HTTP servers are integrated directly into Koa's small ~570 SLOC codebase. This includes things like content negotiation, normalization of node inconsistencies, redirection, and a few others.

Koa is not bundled with any middleware.

Installation

Koa requires node v7.6.0 or higher for ES2015 and async function support.

$ npm install koa

Hello Koa

const Koa= require('koa');
const {koaBody}=require('koa-body');
const cors = require('koa-cors');
const static = require('koa-static');
const mount = require('koa-mount');
const logger =require('koa-logger2');
const {errorHandler} = require('koa-error-handler2');
const fs = require('fs');
const path = require('path');
const jwt = require('koa-jwt');
const router = require('./app/router');
const app = new Koa();
app.use(koaBody({
    multipart:true
}))
app.use(cors({
    origin:'*'
}))
let staticpath = path.join(__dirname,'app/public');
app.use(mount('/public',static(staticpath)));
var log_middleware = logger('ip [day/month/year:time zone] "method url protocol/httpVer" status size "referer" "userAgent" duration ms custom[unpacked]');
log_middleware.setStream(fs.createWriteStream(path.join(__dirname, 'app/log/log.log'), { flags: 'a' }));
app.use(log_middleware.gen);
app.use(jwt({ secret: 'asdfgh' }).unless({ path: [/^\/public/,'/phone/register','/phone/code','/phone/login','/favicon.ico'] }));
app.use(router);
app.use(errorHandler)
app.listen(3000,()=>{
    console.log('服务启动成功')
})

app.js

Getting started

  • Kick-Off-Koa - An intro to Koa via a set of self-guided workshops.
  • Workshop - A workshop to learn the basics of Koa, Express' spiritual successor.
  • Introduction Screencast - An introduction to installing and getting started with Koa

Middleware

Koa is a middleware framework that can take two different kinds of functions as middleware:

  • async function
  • common function

Here is an example of logger middleware with each of the different functions:

async functions (node v7.6+)

var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'localhost',
  user            : 'root',
  password        : '123456',
  database        : '2201a'
});
const query = async(sql,params)=>{
    console.log(sql);
    return await new Promise((resolve, reject) => {
        pool.query(sql,params,function(err,res){
            if(err){
                reject(err);  
            }else{
                resolve(res);
            }
        })
    })
}

module.exports = query;


query.js

Common function

var jwt = require('jsonwebtoken');
const signtoken = async (userdata) => {
    return await jwt.sign({
        data: userdata
    }, 'secret', {
        expiresIn: '2h'
    });
}
help.js

const Redis = require("ioredis");
const redis = new Redis();
module.exports = redis;

redis.js

Koa v1.x Middleware Signature

The middleware signature changed between v1.x and v2.x. The older signature is deprecated.

Old signature middleware support will be removed in v3

Please see the Migration Guide for more information on upgrading from v1.x and using v1.x middleware with v2.x.

Context, Request and Response

//二次封装axios  (鉴权)
// request 发请求
const request = axios.create({
    Timeout: 5000
})
// 添加请求拦截器
request.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么  -- 存储 生成的token (jtw 验证用户信息的加密)
    let token = localStorage.getItem('token');
    // 判断 token 是否存在
    if (token) {
        // 请求头  授权 
        config.headers.Authorization = 'Bearer ' + token;
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
request.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    return response;
}, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么 响应的状态 401  token 过期了 -- 重新登录
    if(error.response.status == '401'){
        alert('token 失效或过期');
        // 返回到登录界面
        location.href='/public/phone/login.html'
    }
    return Promise.reject(error);
});

static/register.js

Koa v1.x Middleware Signature

The middleware signature changed between v1.x and v2.x. The older signature is deprecated.

Old signature middleware support will be removed in v3

Please see the Migration Guide for more information on upgrading from v1.x and using v1.x middleware with v2.x.

Context, Request and Response

Each middleware receives a Koa Context object that encapsulates an incoming http message and the corresponding response to that message. ctx is often used as the parameter name for the context object.

const query = require('../db/query');
const md5 = require('md5');
const CheckPhone = async(params)=>{ 
    let sql = `select * from user where phone = '${params.phone}'`;
    return await query(sql);
}
const Register = async(params)=>{ 
    let password = md5(params.password);
    let sql = `insert into user (account,phone,password) values ('${params.account}','${params.phone}','${password}')`;
    return await query(sql);
}

module.exports = {
    CheckPhone,
    Register
}

service/phone.js

Koa provides a Request object as the request property of the Context.
Koa's Request object provides helpful methods for working with http requests which delegate to an IncomingMessage from the node http module.

const usephone = require('../service/phone');
const jwt = require('../extend/help');
const redis = require('../extend/redis');
const Register = async (ctx) => { 
    let res = await usephone.CheckPhone(ctx.request.body); 
    if (res.length > 0) {
        return ctx.body = {
            code: 403,
            message: "账号已经被注册"
        }
    } else {
        let res = await usephone.Register(ctx.request.body); 
        if (res.affectedRows > 0) {
            return ctx.body = {
                code: 200,
                message: "账号注册成功"
            }
        } else {
            return ctx.body = {
                code: 403,
                message: "账号注册失败"
            }
        }
    }
}
const PhoneCode = async (ctx) => {
    let {
        phone
    } = ctx.request.body;
    if (!phone) {
        return ctx.body = {
            code: 403,
            message: "手机号不能为空"
        }
    }
    let regphone = /^1[3-9]\d{9}$/
    if (!regphone.test(phone)) {
        return ctx.body = {
            code: 403,
            message: "手机号不正确"
        }
    }
    let res = await usephone.CheckPhone(ctx.request.body);
    if (res.length == 0) {
        return ctx.body = {
            code: 403,
            message: "手机号未注册"
        }
    }
    let rediscode = await redis.get(phone);
    if (rediscode == null) {
        let code = Math.random().toFixed(6).slice(-6);
        await redis.setex(phone, 60, code);
        return ctx.body = {
            code: 200,
            message: '验证码发送成功',
            data: code
        }
    } else {
        return ctx.body = {
            code: 403,
            message: '验证码发送频繁',
        }
    }
}
const Login = async (ctx) => {
    let res = await usephone.CheckPhone(ctx.request.body);
    if (res.length == 0) {
        return ctx.body = {
            code: 403,
            message: "手机号未注册"
        }
    }
    let rediscode = await redis.get(ctx.request.body.phone)
    if (rediscode == ctx.request.body.code) {
        let token = await jwt.signtoken(res[0]);
        return ctx.body = {
            code: 200,
            message: "登录成功",
            data: token
        }
    } else {
        return ctx.body = {
            code: 403,
            message: "验证码有误",
        }
    }
}
const GetUserDate = async (ctx) => {
    let userdata = await ctx.state.user.data;
    return ctx.body = {
        code: 200,
        message: "获取成功",
        data: userdata
    }
}
module.exports = {
    Register,
    PhoneCode,
    Login,
    GetUserDate
}

controller/phone.js

Here is an example of checking that a requesting client supports xml.

const Router = require('koa2-router');
// 引入 phone
const phone = require('./controller/phone')

const router = new Router();
// phone
router.post('/phone/register',phone.Register)
// phonecode
router.post('/phone/code',phone.PhoneCode)
// login
router.post('/phone/login',phone.Login)
// userdata
router.get('/phone/user',phone.GetUserDate)



module.exports = router;

router.js

Koa provides a Response object as the response property of the Context.
Koa's Response object provides helpful methods for working with http responses which delegate to a ServerResponse .

Koa's pattern of delegating to Node's request and response objects rather than extending them provides a cleaner interface and reduces conflicts between different middleware and with Node itself as well as providing better support for stream handling. The IncomingMessage can still be directly accessed as the req property on the Context and ServerResponse can be directly accessed as the res property on the Context.

Here is an example using Koa's Response object to stream a file as the response body.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>
        <p>账号 <input type="text" id="account"></p>
        <p>手机 <input type="text" id="phone"></p>
        <p>密码 <input type="text" id="password"></p>
        <button onclick="Register()">注册</button>
    </div>
</body>
<!-- 引入axios -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- 二次封装的 request -->
<script src="../../public/static/request.js"></script>
<script>
    const Register = async () => {
        let data = {
            account: document.querySelector('#account').value,
            phone: document.querySelector('#phone').value,
            password: document.querySelector('#password').value,
        }
        let res = await request.post('/phone/register', data);
        if(res.data.code == 200){
            alert('注册成功')
            location.href='/public/phone/login.html'
        }else{
            alert('注册失败')
        }
    }
</script>

</html>


phone/register.html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>
        <p>手机 <input type="text" id="phone"></p>
        <p>验证码 <input type="text" id="code"></p>
        <p>接收<b id="yzm"></b>倒计时 <span id="djs"></span></p>
        <button onclick="PhoneCode()">发送验证码</button>
        <button onclick="Login()">登录</button>
    </div>
</body>
<!-- 引入axios -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- 二次封装的 request -->
<script src="../../public/static/request.js"></script>
<script>
    // 发送验证码
    const PhoneCode = async () => {
        let data = {
            phone: document.querySelector('#phone').value,
        }
        let res = await request.post('/phone/code', data)
        if (res.data.code == 200) {
            document.querySelector('#yzm').innerHTML = res.data.data;
            let t = 60;
            timer = setInterval(() => {
                t--;
                document.querySelector('#djs').innerHTML = `${t}s`;
                if (t == 0) {
                    clearInterval(timer);
                    return alert('重新发送验证码')
                }
            }, 1000)
        }


    }
    // 登录
    const Login = async () => {
        let data = {
            phone: document.querySelector('#phone').value,
            code: document.querySelector('#code').value,
        }
        let res = await request.post('/phone/login', data);
        if (res.data.code == 200) {
            console.log(res);
            localStorage.setItem('token', res.data.data);
            location.href = '/public/cart/cart.html';
        } else {
            alert('登录失败')
        }
    }
</script>

</html>

phone/login.html

The Context object also provides shortcuts for methods on its request and response. In the prior examples, ctx.type can be used instead of ctx.response.type and ctx.accepts can be used instead of ctx.request.accepts.

For more information on Request, Response and Context, see the Request API Reference, Response API Reference and Context API Reference.

Koa Application

The object created when executing new Koa() is known as the Koa application object.

The application object is Koa's interface with node's http server and handles the registration of middleware, dispatching to the middleware from http, default error handling, as well as configuration of the context, request and response objects.

Learn more about the application object in the Application API Reference.

Documentation

Troubleshooting

Check the Troubleshooting Guide or Debugging Koa in the general Koa guide.

Running tests

$ npm test

Reporting vulnerabilities

To report a security vulnerability, please do not open an issue, as this notifies attackers of the vulnerability. Instead, please email dead_horse, jonathanong, and niftylettuce to disclose.

Authors

See AUTHORS.

Community

Job Board

Looking for a career upgrade?

Backers

Support us with a monthly donation and help us continue our activities.

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site.

License

MIT

1.0.5

1 month ago

1.0.3

1 month ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago