使用Express開發小說API接口服務1.0(二)

使用Express開發小說API接口服務1.0(二)

線上訪問地址https://api.langpz.com/git

以前完成了首頁和搜索的接口,如今就開始寫剩下的接口。github

獲取小說源

由於追書神器正版源是收費加密的,因此只能使用盜版源,因此要封裝一個獲取小說源的接口。
修改app.js 文件路由中間件配置,增長一個路由express

let sourceRouter = require('./routes/source');
app.use('/source', sourceRouter);

在routes下面新建 source.jsjson

let express = require('express');
let request = require('request');
let common = require('../common/common.json'); // 引用公共文件
let router = express.Router();

/** 
  獲取小說源
  返回盜版源和正版源
  param id {String} 是首頁和搜索返回接口 books[i].id
  param n {Number || String}  使用第幾個源,能夠不用傳參默認 1
  http://api.zhuishushenqi.com/atoc?view=summary&book=${bookID}
*/
router.get('/', function (req, res, next) {
    if (!req.query.id) {
        res.send(JSON.stringify({ "flag": 0, "msg": "請傳入ID..." }));
    }
    // req.query.id 編碼轉義
    let id = encodeURI(req.query.id);
    request.get(`${common.API}/atoc?view=summary&book=${id}`, function (err, response, body){
        if(err){
            res.send(JSON.stringify({ "flag": 0, "msg": "請求出錯了..." }));
        }
        
        // 解析返回的數據
        body = JSON.parse(body);
        // 判斷是否返回內容
        if (body.length == 0){
            res.send(JSON.stringify({ "flag": 0, "msg": "沒有獲取到小說源,換個小說看吧" }));
        }

        // 第一個源是正版源,是收費加密的,因此默認選中第二個源
        let n = parseInt(req.query.n);
        if (isNaN(n) || n == 0){
            n = 1;
        }

        // 判斷n是否大於源數據的長度
        if (n >= body.length){
            res.send(JSON.stringify({ "flag": 0, "msg": "n的參數值不正確,沒有那個源" }));
        }else{
            res.send(JSON.stringify({ "flag": 1, "books": body[n], "msg": "OK" }));
        }
    });
});

module.exports = router;

訪問http://localhost:3000/source/?id=50864bf69dacd30e3a000014&n=3 就能夠看到返回第四個源的數據。api

小說文章列表

修改app.js 文件路由中間件配置,增長一個路由app

let chapterRouter = require('./routes/chapter');
app.use('/chapter', chapterRouter);

在routes下面新建 chapter.jsui

let express = require('express');
let request = require('request');
let common = require('../common/common.json'); // 引用公共文件
let router = express.Router();

/** 
  獲取小說文章列表
  返回小說文章列表
  param id {String} 是小說源接口 books.id
  http://api.zhuishushenqi.com/atoc/${id}?view=chapters
*/
router.get('/', function (req, res, next) {
    if (!req.query.id){
        res.send(JSON.stringify({ "flag": 0, "msg": "請傳入ID..." }));
    }
    // req.query.id 編碼轉義
    let id = encodeURIComponent(req.query.id);
    request.get(`${common.API}/atoc/${id}?view=chapters`, function (err, response, body) {
        if (err) {
            res.send(JSON.stringify({ "flag": 0, "msg": "請求出錯了..." }));
        }

        if (body == "wrong param"){
            res.send(JSON.stringify({ "flag": 0, "msg": "傳入錯誤的ID..." }));
        }else{
            // 解析返回的數據
            body = JSON.parse(body);
            if (body.chapters.length > 0) {
                res.send(JSON.stringify({ "flag": 1, "chapters": body.chapters, "msg": "OK" }));
           }
        }
        
    });
});

module.exports = router;

訪問http://localhost:3000/chapter/?id=57416370ccc94e4b41df80cc 就能夠看到數據。id小說源接口返回的id。編碼

小說文章內容

修改app.js 文件路由中間件配置,增長一個路由加密

let articleRouter = require('./routes/article');
app.use('/article', articleRouter);

在routes下面新建 article.jscode

let express = require('express');
let request = require('request');
let common = require('../common/common.json'); // 引用公共文件
let router = express.Router();

/** 
  獲取小說文章內容
  返回小說文章內容
  param link {String} 是小說文章列表接口 chapters[0].link
  http://chapter2.zhuishushenqi.com/chapter/${link}
*/
router.get('/', function (req, res, next) {
    if (!req.query.link) {
        res.send(JSON.stringify({ "flag": 0, "msg": "請傳入link..." }));
    }
    // req.query.link 編碼轉義
    let link = encodeURIComponent(req.query.link);
    request.get(`${common.CHAPTER}/chapter/${link}`, function (err, response, body) {
        if (err) {
            res.send(JSON.stringify({ "flag": 0, "msg": "請求出錯了..." }));
        }

        // 解析返回的數據
        body = JSON.parse(body);

        if (body.ok){
            res.send(JSON.stringify({ "flag": 1, "chapter": body.chapter, "msg": "OK" }));
        }else{
            res.send(JSON.stringify({ "flag": 0, "msg": "傳入link有錯誤" }));
        }
    });
});

module.exports = router;

訪問http://localhost:3000/article?link=http://www.69shu.com/txt/1463... 就能夠看到數據。

排行榜

修改app.js 文件路由中間件配置,增長一個路由

let rankingRouter = require('./routes/ranking');
app.use('/ranking', rankingRouter);

在routes下面新建 ranking.js

let express = require('express');
let request = require('request');
let common = require('../common/common.json'); // 引用公共文件
let router = express.Router();

/** 
  獲取排行榜
  返回排行榜
  param id {String} 沒有傳參數就是獲取所有榜單,不然根據參數獲取榜單
  http://api.zhuishushenqi.com/ranking/gender
  http://api.zhuishushenqi.com/ranking/${id}
*/

router.get('/', function (req, res, next) {
    // 獲取所有榜單
    request.get(`${common.API}/ranking/gender`, function (err, response, body) {
        if (err) {
            res.send(JSON.stringify({ "flag": 0, "msg": "請求出錯了..." }));
        }

        // 解析返回的數據
        body = JSON.parse(body);

        if (body.ok) {
            let ranking = {
                male: body.male,
                picture: body.picture,
                epub: body.epub,
                female: body.female
            };
            res.send(JSON.stringify({ "flag": 1, "ranking": ranking, "msg": "OK" }));
        } else {
            res.send(JSON.stringify({ "flag": 0, "msg": "出錯了" }));
        }
    });
});

router.get('/:id', function (req, res, next) {
    if (req.params.id) {
        // req.param.id 編碼轉義
        let id = encodeURIComponent(req.params.id);

        // 根據id獲取榜單
        request.get(`${common.API}/ranking/${id}`, function (err, response, body) {
            if (err) {
                res.send(JSON.stringify({ "flag": 0, "msg": "請求出錯了..." }));
            }

            // 解析返回的數據
            body = JSON.parse(body);

            if (body.ok) {
                res.send(JSON.stringify({ "flag": 1, "ranking": body.ranking, "msg": "OK" }));
            } else {
                res.send(JSON.stringify({ "flag": 0, "msg": "傳入id錯誤" }));
            }
        });
    }else{
        res.send(JSON.stringify({ "flag": 0, "msg": "id沒有傳" }));
    }
});

module.exports = router;

分別訪問http://localhost:3000/ranking/ 和 http://localhost:3000/ranking/54d43437d47d13ff21cad58b 就能夠獲取到榜單的數據。
1.0版本的開發就告於段落了。

github倉庫訪問地址

https://github.com/lanpangzhi/novel-api

個人博客和GitHub地址

https://github.com/lanpangzhi

http://blog.langpz.com

參考

https://github.com/expressjs/morgan

https://juejin.im/entry/593a3fdf61ff4b006c737ca4

https://github.com/jianhui1012/bookreader/wiki/API-%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3

相關文章
相關標籤/搜索