当前位置: 首页 > 后端技术 > Node.js

使用Express开发新颖的API接口服务1.0(2)

时间:2023-04-03 13:46:34 Node.js

使用Express开发新颖的API接口服务1.0(2)在线访问地址https://api.langpz.com/在完成首页和搜索界面之前,现在开始编写界面的其余部分。获取小说源因为追书神器官方源是收费加密的,只能使用盗版源,所以必须封装一个获取小说源的接口。修改app.js文件路由中间件配置,添加路由letsourceRouter=require('./routes/source');app.use('/source',sourceRouter);创建一个新的source.jsletexpress=require('express');letrequest=require('request');letcommon=require('../common/common.json');//引用普通文件letrouter=express.Router();/**get小说源返回盗版源和正版源。paramid{String}为首页和搜索返回接口books[i].idparamn{Number||.zhuishushushenqi.com/atoc?view=summary&book=${bookID}*/router.get('/',function(req,res,next){if(!req.query.id){res.send(JSON.stringify({"flag":0,"msg":"PleasepassintheID..."}));}//req.query.idencodingescapeletid=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":"Arequesterroroccurred..."}));}//解析返回数据body=JSON.parse(body);//判断是否返回内容if(body.length==0){res.send(JSON.stringify({"flag":0,"msg":"未获取小说出处,请阅读其他小说"}));}//第一个源是合法源,是收费加密的,所以默认选择第二个源letn=parseInt(req.query.n);如果(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可以看到第四个源返回的数据小说文章列表修改app.js文件路由中间件配置,添加路由letchapterRouter=require('./routes/chapter');app.use('/chapter',chapterRouter);在routes=require('express');letrequest=require('request');letcommon=require('../common/common.json');下创建一个新的chapter.jsletexpress//引用普通文件letrouter=express.Router();/**获取小说文章列表,返回小说文章列表paramid{String}为小说来源接口books.idhttp://api.zhuishushenqi.com/atoc/${id}?view=chapters*/router.get('/',function(req,res,next){if(!req.query.id){res.send(JSON.stringify({"flag":0,"msg":"PleasepassintheID..."}));}//req.query.idencodingescapeletid=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=="wrongparam"){res.send(JSON.stringify({"flag":0,"msg":"Input错误ID..."}));}else{//解析返回的数据体=JSON.parse(正文);如果(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文件路由中间件配置,添加路由letarticleRouter=require('./routes/article');app.use('/article',articleRouter);在routes=require('express')下新建一个article.jsletexpress;letrequest=require('request');letcommon=require('../common/common.json');//引用普通文件letrouter=express.Router();/**获取小说文章内容,返回小说文章内容paramlink{String}为小说文章列表接口chapters[0].linkhttp://chapter2.zhuishushushenqi.com/chapter/${link}*/router.get('/',function(req,res,next){if(!req.query.link){res.send(JSON.stringify({"flag":0,"msg":"Pleasepassinlink..."}));}//req.query.linkencodingescapeletlink=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。章节,“消息”:“确定”}));}else{res.send(JSON.stringify({"flag":0,"msg":"传入链接有错误"}));}});});module.exports=路由器;访问http://localhost:3000/article?link=http://www.69shu.com/txt/1463...查看数据Ranking修改app.js文件路由中间件配置,添加路由letrankingRouter=require('./routes/ranking');app.use('/ranking',rankingRouter);在路由下创建一个新的ranking.jsletexpress=require('express');letrequest=require('request');letcommon=require('../common/common.json');//参考普通文件letrouter=express.Router();/**获取排行榜并返回排行榜paramid{String}如果不传参数,则获取所有列表,否则根据参数获取列表http://api.zhuishushenqi.com/ranking/genderhttp://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":"Anerroroccurredintherequest..."}));}//解析返回的数据body=JSON.parse(body);if(body.ok){letranking={male:body.male,图片:body.picture,epub:body.epub,女性:body.female};res.send(JSON.stringify({"flag":1,"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编码转义letid=encodeURIComponent(req.params.id);//获取列表request.get(`${common.API}/ranking/${id}`,function(err,response,body){if(err){res.send(JSON.stringify({"flag":0,"msg":"Arequesterroroccurred..."}));}//解析返回的数据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":"NoidPass"}));}});module.exports=路由器;分别访问http://localhost:3000/ranking/和http://localhost:3000/ranking/54d43437d47d13ff21cad58b可以得到榜单的数据。1.0版本的开发已经告一段落。github仓库访问地址https://github.com/lanpangzhi/novel-api我的博客和github地址https://github.com/lanpangzhihttp://blog.langpz.com参考https://github.com/expressjs/摩根https://juejin.im/entry/593a3fdf61ff4b006c737ca4https://github.com/jianhui1012/bookreader/wiki/API-%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3