Using Express to develop novel API service 1.0 (2)
Online access address https://api.langpz.com/
Before the completion of the home page and search interface, now start to write the rest of the interface.
Access to novel sources
Because the original source of Book Chasing artifact is charged and encrypted, it can only use pirated source, so it is necessary to encapsulate an interface to obtain novel source.
Modify the configuration of app.js file routing middleware to add a route
let sourceRouter = require('./routes/source'); app.use('/source', sourceRouter);
Under routes, create a new source.js
let express = require('express'); let request = require('request'); let common = require('../common/common.json'); // Reference to public documents let router = express.Router(); /** Access to novel sources Return to pirated and genuine sources param id {String} Is the home page and search return interface books[i].id param n {Number || String} Using the first source, you can use the default 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": "Please introduce ID..." })); } // req.query.id code escape 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": "There is an error in the request..." })); } // Parsing returned data body = JSON.parse(body); // Determine whether to return content if (body.length == 0){ res.send(JSON.stringify({ "flag": 0, "msg": "I haven't got the source of the novel. Let's change it to another novel" })); } // The first source is the genuine source, which is charge encrypted, so the second source is selected by default let n = parseInt(req.query.n); if (isNaN(n) || n == 0){ n = 1; } // Determine whether n is greater than the length of source data if (n >= body.length){ res.send(JSON.stringify({ "flag": 0, "msg": "n The parameter value of is incorrect. There is no source" })); }else{ res.send(JSON.stringify({ "flag": 1, "books": body[n], "msg": "OK" })); } }); }); module.exports = router;
Visit http://localhost : 3000 / source /? Id = 50864bf69dacd30e3a0000014 & n = 3, you can see the data returned to the fourth source.
List of novel articles
Modify the configuration of app.js file routing middleware to add a route
let chapterRouter = require('./routes/chapter'); app.use('/chapter', chapterRouter);
Under routes, create a new chapter.js
let express = require('express'); let request = require('request'); let common = require('../common/common.json'); // Reference to public documents let router = express.Router(); /** Get a list of novel articles Return to the list of novel articles param id {String} Is the novel source interface 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": "Please introduce ID..." })); } // req.query.id code escape 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": "There is an error in the request..." })); } if (body == "wrong param"){ res.send(JSON.stringify({ "flag": 0, "msg": "Incoming bad ID..." })); }else{ // Parsing returned data body = JSON.parse(body); if (body.chapters.length > 0) { res.send(JSON.stringify({ "flag": 1, "chapters": body.chapters, "msg": "OK" })); } } }); }); module.exports = router;
Visit http://localhost : 3000 / chapter /? id = 57416370ccc94e41b41df80cc to see the data. id the id returned by the novel source interface.
The content of the novel
Modify the configuration of app.js file routing middleware to add a route
let articleRouter = require('./routes/article'); app.use('/article', articleRouter);
Create article.js under routes
let express = require('express'); let request = require('request'); let common = require('../common/common.json'); // Reference to public documents let router = express.Router(); /** Get the content of the novel Return to the content of the novel param link {String} Is the novel article list interface 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": "Please introduce link..." })); } // req.query.link code escape 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": "There is an error in the request..." })); } // Parsing returned data 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": "afferent link Erroneous" })); } }); }); module.exports = router;
Visit http://localhost:3000/article?link=http://www.69shu.com/txt/1463... You can see the data.
Ranking List
Modify the configuration of app.js file routing middleware to add a route
let rankingRouter = require('./routes/ranking'); app.use('/ranking', rankingRouter);
Create ranking.js under routes
let express = require('express'); let request = require('request'); let common = require('../common/common.json'); // Reference to public documents let router = express.Router(); /** Get leaderboards Back to Leaderboard param id {String} If no parameter is passed, all the lists will be obtained. Otherwise, the list will be obtained according to the parameter http://api.zhuishushenqi.com/ranking/gender http://api.zhuishushenqi.com/ranking/${id} */ router.get('/', function (req, res, next) { // Get all the lists request.get(`${common.API}/ranking/gender`, function (err, response, body) { if (err) { res.send(JSON.stringify({ "flag": 0, "msg": "There is an error in the request..." })); } // Parsing returned data 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": "Error" })); } }); }); router.get('/:id', function (req, res, next) { if (req.params.id) { // req.param.id code escape let id = encodeURIComponent(req.params.id); // Get the list according to id request.get(`${common.API}/ranking/${id}`, function (err, response, body) { if (err) { res.send(JSON.stringify({ "flag": 0, "msg": "There is an error in the request..." })); } // Parsing returned data 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": "afferent id error" })); } }); }else{ res.send(JSON.stringify({ "flag": 0, "msg": "id No pass" })); } }); module.exports = router;
Separate visits http://localhost : 3000/ranking / and http://localhost : 3000/ranking/54d43437d47d13ff21cad58b can get the data of the list.
The development of version 1.0 is over.
github warehouse access address
https://github.com/lanpangzhi/novel-api
My blog and GitHub address
Reference resources
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