使用Express开发小说API接口服务1.0(一) 1.0版本技术栈使用express-generator、express、request、morgan、file-stream-rotator。接口用追书神器API。
github创建仓库 先创建一个仓库放文件
然后克隆创建好的仓库
1 git clone  https ://github.com/lanpangzhi/novel-api.git 
安装 express-generator 快速生成项目 1 npm install  -g express-generator 
然后再之前克隆仓库的上一级目录执行
1 2 3 4 5 6 7 8 express --no -view  novel-api cd  novel-apinpm install  npm install request file -stream-rotator -S  DEBUG=novel-api:* & npm start set  DEBUG=novel-api:* & npm start
生成好的目录结构和文件
设置cors 跨域 打开项目根目录app.js,放在路由上面。
1 2 3 4 5 6 7 8 app.all('*', function (req, res, next) {     res.header("Access-Control-Allow-Origin" , "*" );     res.header("Access-Control-Allow-Headers" , "Origin, X-Requested-With, Content-Type, Accept" );     res.header("Access-Control-Allow-Methods" , "PUT ,POST ,GET ,DELETE ,OPTIONS " );     res.header("X-Powered-By" , ' 3 .2 .1 ')     res.header("Content-Type" , "application/json;charset=utf-8" );     next() }); 
日志写入本地文件 按时间分割log日志并写入本地磁盘,需要在app.js文件中引入fs和file-stream-rotator模块。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 let  fs = require ('fs' ); let  FileStreamRotator = require ('file-stream-rotator' );  let  logDir = path.join (__dirname, 'log' );fs.existsSync(logDir) || fs.mkdirSync(logDir); let  accessLogStream = FileStreamRotator.getStream({    date_format: 'YYYYMMDD' ,     filename: path.join (logDir, 'access-%DATE%.log' ),     frequency: 'daily' ,     verbose: false  }); app.use(logger('combined' , { stream: accessLogStream })); 
创建公共文件 项目根目录创建common文件夹,再里面再新建一个common.json文件
1 2 3 4 5 {     "API" : "http://api.zhuishushenqi.com" ,     "PIC" : "http://statics.zhuishushenqi.com" ,     "CHAPTER" : "http://chapter2.zhuishushenqi.com"  } 
API域名: http://api.zhuishushenqi.com http://statics.zhuishushenqi.com http://chapter2.zhuishushenqi.com 
首页接口 1.0版本首页接口直接返回最热榜前20条数据。
1 app.use ('/index' , indexRouter); 
修改routes/index.js 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 let  express = require ('express' );let  request = require ('request' );let  common = require ('../common/common.json' ); let  router = express.Router();  首页数据追书最热榜 Top100   获取单一排行榜   http://api.zhuishushenqi.com/ranking/{rankingId} */ router.get('/' , function (req, res, next )       request.get(`${common.API} /ranking/54d42d92321052167dfb75e3` , function  (error, response, body )      if  (error){       res.send(JSON .stringify({"flag" : 0 , "msg" : "请求出错了..." }));     }               body = JSON .parse(body);     if  (body.ok){       let  books = body.ranking.books.slice(0 , 19 );       books.forEach(element  =>         element.cover = common.PIC + element.cover;       });       res.send(JSON .stringify({ "flag" : 1 , "books" : books, "msg" : "OK"  }));     }else {       res.send(JSON .stringify({ "flag" : 0 , "msg" : "rankingId有问题"  }));     }     }); }); module .exports = router;
访问http://localhost:3000/index  就可以看到返回的数据了。
搜索接口 1.0版本的搜索接口只取前40条数据,可以模糊查询。
1 2 let  searchRouter = require ('./routes/search' );app.use ('/search' , searchRouter); 
然后把routes文件夹下面的users.js删除,新建search.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 let  express = require ('express' );let  request = require ('request' );let  common = require ('../common/common.json' ); let  router = express.Router();  模糊搜索接口   返回模糊搜索前40条数据   http://api.zhuishushenqi.com/book/fuzzy-search?query={name} */ router.get('/' , function (req, res, next )       if  (req.query.query){          let  query = encodeURIComponent (req.query.query);     request.get(`${common.API} /book/fuzzy-search?query=${query} ` , function  (error, response, body )        if  (error){         res.send(JSON .stringify({ "flag" : 0 , "msg" : "请求出错了..."  }));       }               body = JSON .parse(body);       if  (body.ok){         if  (body.books.length == 0 ){           res.send(JSON .stringify({ "flag" : 0 , "msg" : "没有找到书籍,换个名字试试吧。"  }));         }                           let  books = body.books.slice(0 , 39 );         books.forEach(element  =>           element.cover = common.PIC + element.cover;         });         res.send(JSON .stringify({ "flag" : 1 , "books" : books, "msg" : "OK"  }));       }else {         res.send(JSON .stringify({ "flag" : 0 , "msg" : "请求出错了..."  }));       }     });   }else {     res.send(JSON .stringify({"flag" : 0 , "msg" : "请传入query参数" }));   }    }); module .exports = router;
访问http://localhost:3000/search/?query=遮天  就可以看到返回的数据了。
参考 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