[Express] Designing the Application to be Extensible
Features and Modularity
In this concept, we dive into splitting our code into logical "features". This is a way of describing modular programming where the code which relates to one task is grouped together. In this example, the /feed
and /auth
endpoints are our features. All routing, models, business logic, and helper functions for these modules should be separated as much as possible. This way, we have an understanding of where code responsibilities just by looking at the general structure.
Often one feature may depend on another feature - for example, user authentication will be needed within the feed. But these dependencies should be kept loose and consistent with only a few methods being consumed. The goal as the system continues to grow, is to minimize refactoring outside of specific features. As you continue to learn cloud and explore microservices, often entire features might be ported to their own servers infrastructure making this loose coupling even more critical.
server.ts:
... app.use('/api/v0/', IndexRouter) // Root URI call app.get( "/", async ( req, res ) => { res.send( "/api/v0/" ); } ); ...
index.router.ts:
import { Router, Request, Response } from 'express'; import { FeedRouter } from './feed/routes/feed.router'; import { UserRouter } from './users/routes/user.router'; const router: Router = Router(); router.use('/feed', FeedRouter); router.use('/users', UserRouter); router.get('/', async (req: Request, res: Response) => { res.send(`V0`); }); export const IndexRouter: Router = router;
fee.router.ts:
import { Router, Request, Response } from 'express'; import { FeedItem } from '../models/FeedItem'; import { requireAuth } from '../../users/routes/auth.router'; import * as AWS from '../../../../aws'; const router: Router = Router(); // Get all feed items router.get('/', async (req: Request, res: Response) => { const items = await FeedItem.findAndCountAll({order: [['id', 'DESC']]}); items.rows.map((item) => { if(item.url) { item.url = AWS.getGetSignedUrl(item.url); } }); res.send(items); }); ... export const FeedRouter: Router = router;
相关文章
- 【已解决】spring boot项目从mysql中获取数据候错误The driver has not received any packets from the server
- 'django.contrib.auth.middleware.AuthenticationMiddleware' must be in MIDDLEWARE in order to use the
- ORA-26747: The one-to-many transformation function string encountered the following error: string ORACLE 报错 故障修复 远程处理
- ORA-30160: Unable to access the file ORACLE 报错 故障修复 远程处理
- ORA-32582: table function with left correlation to a table cannot also be left outer-joined to the table ORACLE 报错 故障修复 远程处理
- ORA-48120: error encountered when attempting to retrieve the file size [string] [string] ORACLE 报错 故障修复 远程处理
- ORA-48486: The file [string] exceeds the maximum length [string] ORACLE 报错 故障修复 远程处理
- ORA-53410: The attribute string does not conform to the VM rule. ORACLE 报错 故障修复 远程处理
- ORA-02762: file number to be cancelled is greater than the maximum. ORACLE 报错 故障修复 远程处理
- ORA-04009: MAXVALUE cannot be made to be less than the current value ORACLE 报错 故障修复 远程处理
- ORA-13486: invalid or insufficient GeoTIFF metadata to georeference the GeoRaster object: string ORACLE 报错 故障修复 远程处理
- ORA-13634: The task string needs to be reset before being re-executed. ORACLE 报错 故障修复 远程处理
- ORA-13635: The value provided for parameter string cannot be converted to a number. ORACLE 报错 故障修复 远程处理
- ORA-15743: need to specify the object ID if the tuning task is on a SQL set. ORACLE 报错 故障修复 远程处理
- ORA-16086: Redo data cannot be written to the standby redo log ORACLE 报错 故障修复 远程处理
- ORA-16795: the standby database needs to be re-created ORACLE 报错 故障修复 远程处理
- Mysql主从架构报错-Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work…详解数据库
- The Power of ‘if z’ in the Linux World
- The Comprehensive Guide to Embedded Linux(嵌入式linux手册)
- Exploring the World of Linux with Ease: A Guide to Using Aoyou(遨游forlinux)
- Exploring the World of MySQL: Understanding How to Query the Current Database(mysql查询当前库)