13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

从一个优质开源项目来看前端架构

何为系统架构师?

这是百度百科的答案

大多数人的问题

如何成为一名前端架构师?

正式开始

我们从一个比较不错的项目入手,谈谈一个前端架构师要做什么

 为了阅读的舒适度,我把下面的正文尽量口语化一点

先把代码搞下来

 
 
 
 
  1. git clone https://github.com/r-spacex/SpaceX-API.git 

找到 package.json 文件的几个关键点:

 
 
 
 
  1. "main": "server.js", 
  2.    "scripts": { 
  3.     "test": "npm run lint && npm run check-dependencies && jest --silent --verbose", 
  4.     "start": "node server.js", 
  5.     "worker": "node jobs/worker.js", 
  6.     "lint": "eslint .", 
  7.     "check-dependencies": "npx depcheck --ignores=\"pino-pretty\"" 
  8.   }, 
 
 
 
 
  1. "koa": "^2.13.0", 
  2.     "koa-bodyparser": "^4.3.0", 
  3.     "koa-conditional-get": "^3.0.0", 
  4.     "koa-etag": "^4.0.0", 
  5.     "koa-helmet": "^6.0.0", 
  6.     "koa-pino-logger": "^3.0.0", 
  7.     "koa-router": "^10.0.0", 
  8.     "koa2-cors": "^2.0.6", 
  9.     "lodash": "^4.17.20", 
  10.     "moment-range": "^4.0.2", 
  11.     "moment-timezone": "^0.5.32", 
  12.     "mongoose": "^5.11.8", 
  13.     "mongoose-id": "^0.1.3", 
  14.     "mongoose-paginate-v2": "^1.3.12", 
  15.     "eslint": "^7.16.0", 
  16.     "eslint-config-airbnb-base": "^14.2.1", 
  17.     "eslint-plugin-import": "^2.22.1", 
  18.     "eslint-plugin-jest": "^24.1.3", 
  19.     "eslint-plugin-mongodb": "^1.0.0", 
  20.     "eslint-plugin-no-secrets": "^0.6.8", 
  21.     "eslint-plugin-security": "^1.4.0", 
  22.     "jest": "^26.6.3", 
  23.     "pino-pretty": "^4.3.0" 

这里强调一点,如果你的代码需要两人及以上维护,我就强烈建议你不要使用任何黑魔法,以及不使用非主流的库,除非你编写核心底层逻辑时候非用不可(这个时候应该只有你维护)

项目目录 

逐个分析

从项目依赖安装说起

 
 
 
 
  1. "dependencies": { 
  2.     "blake3": "^2.1.4", 
  3.     "cheerio": "^1.0.0-rc.3", 
  4.     "cron": "^1.8.2", 
  5.     "fuzzball": "^1.3.0", 
  6.     "got": "^11.8.1", 
  7.     "ioredis": "^4.19.4", 
  8.     "koa": "^2.13.0", 
  9.     "koa-bodyparser": "^4.3.0", 
  10.     "koa-conditional-get": "^3.0.0", 
  11.     "koa-etag": "^4.0.0", 
  12.     "koa-helmet": "^6.0.0", 
  13.     "koa-pino-logger": "^3.0.0", 
  14.     "koa-router": "^10.0.0", 
  15.     "koa2-cors": "^2.0.6", 
  16.     "lodash": "^4.17.20", 
  17.     "moment-range": "^4.0.2", 
  18.     "moment-timezone": "^0.5.32", 
  19.     "mongoose": "^5.11.8", 
  20.     "mongoose-id": "^0.1.3", 
  21.     "mongoose-paginate-v2": "^1.3.12", 
  22.     "pino": "^6.8.0", 
  23.     "tle.js": "^4.2.8", 
  24.     "tough-cookie": "^4.0.0" 
  25.   }, 
  26.   "devDependencies": { 
  27.     "eslint": "^7.16.0", 
  28.     "eslint-config-airbnb-base": "^14.2.1", 
  29.     "eslint-plugin-import": "^2.22.1", 
  30.     "eslint-plugin-jest": "^24.1.3", 
  31.     "eslint-plugin-mongodb": "^1.0.0", 
  32.     "eslint-plugin-no-secrets": "^0.6.8", 
  33.     "eslint-plugin-security": "^1.4.0", 
  34.     "jest": "^26.6.3", 
  35.     "pino-pretty": "^4.3.0" 
  36.   }, 

项目目录划分

正式开始看代码

app.js入口文件

 
 
 
 
  1. //组件挂载 
  2. componentDidmount(){ 
  3.  
  4. //组件需要更新时 
  5. shouldComponentUpdate(){ 
  6.  
  7. //组件将要卸载 
  8. componentWillUnmount(){ 
  9.  
  10. ... 
  11. render(){} 

router的代码,简介明了

 
 
 
 
  1. const Router = require('koa-router'); 
  2. const admin = require('./admin/routes'); 
  3. const capsules = require('./capsules/routes'); 
  4. const cores = require('./cores/routes'); 
  5. const crew = require('./crew/routes'); 
  6. const dragons = require('./dragons/routes'); 
  7. const landpads = require('./landpads/routes'); 
  8. const launches = require('./launches/routes'); 
  9. const launchpads = require('./launchpads/routes'); 
  10. const payloads = require('./payloads/routes'); 
  11. const rockets = require('./rockets/routes'); 
  12. const ships = require('./ships/routes'); 
  13. const users = require('./users/routes'); 
  14. const company = require('./company/routes'); 
  15. const roadster = require('./roadster/routes'); 
  16. const starlink = require('./starlink/routes'); 
  17. const history = require('./history/routes'); 
  18. const fairings = require('./fairings/routes'); 
  19.  
  20. const v4 = new Router({ 
  21.   prefix: '/v4', 
  22. }); 
  23.  
  24. v4.use(admin.routes()); 
  25. v4.use(capsules.routes()); 
  26. v4.use(cores.routes()); 
  27. v4.use(crew.routes()); 
  28. v4.use(dragons.routes()); 
  29. v4.use(landpads.routes()); 
  30. v4.use(launches.routes()); 
  31. v4.use(launchpads.routes()); 
  32. v4.use(payloads.routes()); 
  33. v4.use(rockets.routes()); 
  34. v4.use(ships.routes()); 
  35. v4.use(users.routes()); 
  36. v4.use(company.routes()); 
  37. v4.use(roadster.routes()); 
  38. v4.use(starlink.routes()); 
  39. v4.use(history.routes()); 
  40. v4.use(fairings.routes()); 
  41.  
  42. module.exports = v4; 

模块众多,找几个代表性的模块

 
 
 
 
  1. const Router = require('koa-router'); 
  2. const { auth, authz, cache } = require('../../../middleware'); 
  3.  
  4. const router = new Router({ 
  5.   prefix: '/admin', 
  6. }); 
  7.  
  8. // Clear redis cache 
  9. router.delete('/cache', auth, authz('cache:clear'), async (ctx) => { 
  10.   try { 
  11.     await cache.redis.flushall(); 
  12.     ctx.status = 200; 
  13.   } catch (error) { 
  14.     ctx.throw(400, error.message); 
  15.   } 
  16. }); 
  17.  
  18. // Healthcheck 
  19. router.get('/health', async (ctx) => { 
  20.   ctx.status = 200; 
  21. }); 
  22.  
  23. module.exports = router; 

这里补充一个小细节

回到admin

 
 
 
 
  1. /** 
  2.  * Authentication middleware 
  3.  */ 
  4. module.exports = async (ctx, next) => { 
  5.   const key = ctx.request.headers['spacex-key']; 
  6.   if (key) { 
  7.     const user = await db.collection('users').findOne({ key }); 
  8.     if (user?.key === key) { 
  9.       ctx.state.roles = user.roles; 
  10.       await next(); 
  11.       return; 
  12.     } 
  13.   } 
  14.   ctx.status = 401; 
  15.   ctx.body = 'https://youtu.be/RfiQYRn7fBg'; 
  16. }; 
 
 
 
 
  1. // Clear redis cache 
  2. router.delete('/cache', auth, authz('cache:clear'), async (ctx) => { 
  3.   try { 
  4.     await cache.redis.flushall(); 
  5.     ctx.status = 200; 
  6.   } catch (error) { 
  7.     ctx.throw(400, error.message); 
  8.   } 
  9. }); 
 
 
 
 
  1. /** 
  2.  * Error handler middleware 
  3.  * 
  4.  * @param   {Object}    ctx       Koa context 
  5.  * @param   {function}  next      Koa next function 
  6.  * @returns {void} 
  7.  */ 
  8. module.exports = async (ctx, next) => { 
  9.   try { 
  10.     await next(); 
  11.   } catch (err) { 
  12.     if (err?.kind === 'ObjectId') { 
  13.       err.status = 404; 
  14.     } else { 
  15.       ctx.status = err.status || 500; 
  16.       ctx.body = err.message; 
  17.     } 
  18.   } 
  19. }; 

补一张koa洋葱圈的图

再接下来看其他的services

 
 
 
 
  1. // Get one history event 
  2. router.get('/:id', cache(300), async (ctx) => { 
  3.   const result = await History.findById(ctx.params.id); 
  4.   if (!result) { 
  5.     ctx.throw(404); 
  6.   } 
  7.   ctx.status = 200; 
  8.   ctx.body = result; 
  9. }); 
  10.  
  11. // Query history events 
  12. router.post('/query', cache(300), async (ctx) => { 
  13.   const { query = {}, options = {} } = ctx.request.body; 
  14.   try { 
  15.     const result = await History.paginate(query, options); 
  16.     ctx.status = 200; 
  17.     ctx.body = result; 
  18.   } catch (error) { 
  19.     ctx.throw(400, error.message); 
  20.   } 
  21. }); 


通过这个项目,我们能学到什么

成为一个优秀前端架构师的几个技能点


当前名称:从一个优质开源项目来看前端架构
当前网址:http://cdbrznjsb.com/article/cdgdjdc.html

其他资讯

让你的专属顾问为你服务