Giter VIP home page Giter VIP logo

koa2-cet's Introduction

koa2-cet

基于Angular和Koa2的英语四六级成绩查询系统,提供免费API接口

预览

在线预览地址: https://cet.lenshen.com

技术栈

  • Angular:实现前端页面构建
  • Koa2:实现服务端具体业务逻辑
  • ES6ES7ES8:服务端使用ES6语法,promise/async/await 处理异步
  • superagent:爬虫的核心,进行模拟请求
  • cheerio:解析DOM结构,爬取需要的数据
  • cors:服务端返回数据时做了cors设置,允许跨域
  • jsonp:支持JSONP请求,客户端需要传入回调函数名称
  • pm2:服务端使用pm2部署,常驻进程,比forever好用得多(https://github.com/Unitech/pm2)
  • nginx:服务端代理端口转发

使用说明

使用cnpm i 安装所有依赖,然后运行npm run dev,浏览器打开 http://localhost:8001

API接口

本系统免费提供API接口,具体接口如下所示:

URL: https://cet.lenshen.com/api/search?user=姓名&number=准考证号
参数说明:
    user  姓名(需要先将中文进行urlencode编码)
    number  准考证号
请求方式: GET
请求成功返回json:
{ 
  "code":200,
  "message":"查询成功",
  "data":{  
  	"name":"成景文",   //姓名
  	"school":"山西大学",  //学校
  	"type":"英语四级",  //考试类别
	  "number":"140010171105929",  //准考证号
	  "total":"402",   //总分
	  "listen":"107",   //听力
	  "read":"153",  //阅读
	  "writing":"142"  //写作和翻译
  }
}
请求失败返回json:
{ 
  "code":400,
  "message":"查询失败,请检查你的信息是否无误"
}
注意:以上接口可以使用后台代理请求数据,也可以直接使用ajax/fetch/axios请求数据(因为设置了cors)



如果使用JSONP,则需要在url里传入callback:
URL:https://cet.lenshen.com/api/search?callback=cb&&number=准考证号&user=姓名
参数说明:  
    callback  回调函数名称
    user  姓名 
    number  准考证号
请求方式: GET
请求成功返回jsonp:
cb({ 
  "code":200,
  "message":"查询成功",
  "data":{  
    "name":"成景文",   //姓名
    "school":"山西大学",  //学校
    "type":"英语四级",  //考试类别
    "number":"140010171105929",  //准考证号
    "total":"402",   //总分
    "listen":"107",   //听力
    "read":"153",  //阅读
    "writing":"142"  //写作和翻译
  }
})
请求失败返回jsonp:
cb({ 
	"code":400,
	"message":"查询失败,请检查你的信息是否无误"
})

测试用户如下:

姓名:成景文

准考证号:140010171105929

FAQ

若使用的过程中遇到问题,可以加官方群交流:611212696

koa2-cet's People

Contributors

lensh avatar lensh1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

koa2-cet's Issues

Koa2里获取请求数据

一、获取get请求数据

在koa中,获取GET请求数据源头是koa中request对象中的query方法或querystring方法,query返回是格式化好的参数对象,querystring返回的是请求字符串,由于ctx对request的API有直接引用的方式,所以获取GET请求数据有两个途径。

1.是从上下文中直接获取
请求对象ctx.query,返回如 { a:1, b:2 }
请求字符串 ctx.querystring,返回如 a=1&b=2
2.是从上下文的request对象中获取
请求对象ctx.request.query,返回如 { a:1, b:2 }
请求字符串 ctx.request.querystring,返回如 a=1&b=2

例子:

const Koa = require('koa')
const app = new Koa()

const Koa = require('koa')
const app = new Koa()

app.use( async ( ctx ) => {
  let url = ctx.url
  // 从上下文的request对象中获取
  let request = ctx.request
  let req_query = request.query
  let req_querystring = request.querystring

  // 从上下文中直接获取
  let ctx_query = ctx.query
  let ctx_querystring = ctx.querystring
  
  ctx.body = {
    url,
    req_query,
    req_querystring,
    ctx_query,
    ctx_querystring
  }
})

app.listen(3000)
console.log('[demo] request get is starting at port 3000')

二、获取POST请求数据
对于POST请求的处理,koa2没有封装获取参数的方法,需要通过解析上下文context中的原生node.js请求对象req,将POST表单数据解析成query string(例如:a=1&b=2&c=3),再将query string 解析成JSON格式(例如:{"a":"1", "b":"2", "c":"3"})

注意:ctx.request是context经过封装的请求对象,ctx.req是context提供的node.js原生HTTP请求对象,同理ctx.response是context经过封装的响应对象,ctx.res是context提供的node.js原生HTTP请求对象。

对于POST请求的处理,koa-bodyparser中间件可以把koa2上下文的formData数据解析到ctx.request.body中。
安装koa2版本的koa-bodyparser@3中间件:npm install --save koa-bodyparser@3

// 使用ctx.body解析中间件
const Koa = require('koa')
const app = new Koa()
const bodyParser = require('koa-bodyparser')

// 使用ctx.body解析中间件
app.use(bodyParser())

app.use( async ( ctx ) => {

  if ( ctx.url === '/' && ctx.method === 'GET' ) {
    // 当GET请求时候返回表单页面
    let html = `
      <h1>koa2 request post demo</h1>
      <form method="POST" action="/">
        <p>userName</p>
        <input name="userName" /><br/>
        <p>nickName</p>
        <input name="nickName" /><br/>
        <p>email</p>
        <input name="email" /><br/>
        <button type="submit">submit</button>
      </form>
    `
    ctx.body = html
  } else if ( ctx.url === '/' && ctx.method === 'POST' ) {
    // 当POST请求的时候,中间件koa-bodyparser解析POST表单里的数据,并显示出来
    let postData = ctx.request.body
    ctx.body = postData
  } else {
    // 其他请求显示404
    ctx.body = '<h1>404!!! o(╯□╰)o</h1>'
  }
})

app.listen(3000)
console.log('[demo] request post is starting at port 3000')

总结:
(1)get请求:使用 ctx.query
(2)post请求:使用ctx.request.body

利用superagent来写爬虫

一、什么是superagent?
superagent它是一个强大并且可读性很好的轻量级ajaxAPI,是一个关于HTTP方面的一个库,而且它可以将链式写法玩的出神入化。

var superagent = require('superagent');

    superagent
        .post('/api')
        .send({
            'key': 'value'
        })
        .set('header_key', 'header_value')
        .end(function(err, res) {
            if (err) {
                //do something
            } else {
                //do something
            }
        })

四六级查询接口

兄弟,这个查询接口现在是可用的吗?我们学校想做个查询,然后后台正好是用的egg,想借用这个查询方式,可行的话借鉴一下可以吗?

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.