Gathering detailed insights and metrics for express-route-tracker
Gathering detailed insights and metrics for express-route-tracker
Gathering detailed insights and metrics for express-route-tracker
Gathering detailed insights and metrics for express-route-tracker
npm install express-route-tracker
Typescript
Module System
Node Version
NPM Version
67.9
Supply Chain
94.5
Quality
91.9
Maintenance
100
Vulnerability
100
License
Total Downloads
3,777
Last Day
3
Last Week
67
Last Month
750
Last Year
3,777
Minified
Minified + Gzipped
Latest Version
2.0.74
Package Id
express-route-tracker@2.0.74
Unpacked Size
30.62 kB
Size
9.15 kB
File Count
23
NPM Version
11.0.0
Node Version
20.17.0
Publised On
20 Jan 2025
Cumulative downloads
Total Downloads
Last day
50%
3
Compared to previous day
Last week
-61%
67
Compared to previous week
Last month
-75.2%
750
Compared to previous month
Last year
0%
3,777
Compared to previous year
3
2
Express Route Tracker A lightweight library for Express.js that adds route tracking and HATEOAS (Hypermedia as the Engine of Application State) capabilities to your API.
https://github.com/phamhung075/AIanalist
https://github.com/phamhung075/AIanalist
1npm install express-route-tracker 2# or 3yarn add express-route-tracker
1// src/app.ts 2import { RouteDisplay } from 'express-route-tracker'; 3import express from 'express'; 4 5const app = express(); 6app.use("/", router); 7 8// Display all routes in console when starting the app 9const routeDisplay = new RouteDisplay(app); 10routeDisplay.displayRoutes();
1//src\modules\index.ts 2import { Request, Response, NextFunction } from 'express'; 3import { Router } from 'express'; 4 5const router = Router(); 6 7 8router.use('/api/contact', require('./contact')); //<-- need add this line for each module 9 10// router.use('/v1/api/error', require('./error')); 11router.post('/', (_req: Request, res: Response, _next: NextFunction) => { 12 return res.status(200).json({ 13 message: 'Welcome !' 14 }) 15}); 16 17 18export default router;
In your route module (e.g., src/modules/contact/index.ts
):
1// src/modules/contact/index.ts 2import { createHATEOASMiddleware, createRouter } from 'express-route-tracker'; 3import { 4 createContactHandler, 5 deleteContactHandler, 6 getAllContactsHandler, 7 getContactByIdHandler, 8 updateContactHandler 9} from './contact.handler'; 10import { asyncHandler } from '@/_core/helper/asyncHandler'; 11import { config } from '@/_core/config/dotenv.config'; 12 13// Create router with source tracking 14const router = createRouter(__filename); // replace const router = express.Router(); 15 16// Define routes without baseApi prefix 17router.post('/', asyncHandler(createContactHandler)); 18router.get('/', asyncHandler(getAllContactsHandler)); 19router.get('/:id', asyncHandler(getContactByIdHandler)); 20router.put('/:id', asyncHandler(updateContactHandler)); 21router.delete('/:id', asyncHandler(deleteContactHandler)); 22 23export = router; // replace export default router;
In your route module (e.g., src/modules/contact/index.ts
):
1// src/modules/contact/index.ts 2import { createHATEOASMiddleware, createRouter } from 'express-route-tracker'; 3import { 4 createContactHandler, 5 deleteContactHandler, 6 getAllContactsHandler, 7 getContactByIdHandler, 8 updateContactHandler 9} from './contact.handler'; 10import { asyncHandler } from '@/_core/helper/asyncHandler'; 11import { config } from '@/_core/config/dotenv.config'; 12 13// Create router with source tracking 14const router = createRouter(__filename); // replace const router = express.Router(); 15 16router.use(createHATEOASMiddleware(router, { 17 autoIncludeSameRoute: true, 18 baseUrl: config.baseUrl, 19 includePagination: true, 20 customLinks: { 21 documentation: (_req) => ({ 22 rel: 'documentation', 23 href: config.baseUrl+'/docs', 24 method: 'GET', 25 'title': 'API Documentation' 26 }) 27 } 28})); 29 30// Define routes without baseApi prefix 31router.post('/', asyncHandler(createContactHandler)); 32router.get('/', asyncHandler(getAllContactsHandler)); 33router.get('/:id', asyncHandler(getContactByIdHandler)); 34router.put('/:id', asyncHandler(updateContactHandler)); 35router.delete('/:id', asyncHandler(deleteContactHandler)); 36 37export = router; // replace export default router;
Your API responses will now automatically include HATEOAS links:
1{ 2 "id": "yQg9OD4KRTNywa2fHwxN", 3 "name": "John Doe", 4 "links": { 5 "self": { 6 "rel": "self", 7 "href": "localhost:3333/api/contact/yQg9OD4KRTNywa2fHwxN", 8 "method": "GET" 9 }, 10 "create": { 11 "title": "POST /", 12 "rel": "create", 13 "href": "localhost:3333/api/contact/", 14 "method": "POST" 15 }, 16 "collection": { 17 "title": "GET /", 18 "rel": "collection", 19 "href": "localhost:3333/api/contact/", 20 "method": "GET" 21 }, 22 "item": { 23 "title": "GET /:id", 24 "rel": "item", 25 "href": "localhost:3333/api/contact/yQg9OD4KRTNywa2fHwxN", 26 "method": "GET" 27 }, 28 "update": { 29 "title": "PUT /:id", 30 "rel": "update", 31 "href": "localhost:3333/api/contact/yQg9OD4KRTNywa2fHwxN", 32 "method": "PUT" 33 }, 34 "delete": { 35 "title": "DELETE /:id", 36 "rel": "delete", 37 "href": "localhost:3333/api/contact/yQg9OD4KRTNywa2fHwxN", 38 "method": "DELETE" 39 }, 40 "documentation": { 41 "rel": "documentation", 42 "href": "localhost:3333/docs", 43 "method": "GET", 44 "title": "API Documentation" 45 } 46 } 47}
The createHATEOASMiddleware
accepts several options:
autoIncludeSameRoute
: When true, includes all routes from the same file in the linksbaseUrl
: The base URL for generating absolute URLsincludePagination
: Adds pagination links when response includes pagination datacustomLinks
: Custom link generators for additional relationshipsWhen includePagination
is enabled and your response includes pagination data:
1{ 2 data: items, 3 pagination: { 4 currentPage: 1, 5 totalPages: 5 6 }, 7 links: { 8 // Regular links... 9 first: { rel: 'first', href: '/api/contacts?page=1', method: 'GET' }, 10 next: { rel: 'next', href: '/api/contacts?page=2', method: 'GET' }, 11 last: { rel: 'last', href: '/api/contacts?page=5', method: 'GET' } 12 } 13}
createRouter(filename: string)
filename
(string): The source file name (use __filename
).express.Router
Each route handler will have:__source
: Path of the source file.__name
: HTTP method and path.routeLoggerMiddleware
createHATEOASMiddleware
1// config.ts 2export const config = { 3 baseUrl: process.env.API_BASE_URL || 'http://localhost:3333', 4 baseApi: '/api' 5};
Meaningful Relationships: Use semantic rel values that describe the relationship:
self
: Current resourcecollection
: List endpointcreate
: Creation endpointupdate
: Update endpointdelete
: Delete endpointError Handling: Ensure your error responses also include appropriate HATEOAS links:
1function errorHandler(err: Error, req: Request, res: Response, next: NextFunction) { 2 res.status(500).json({ 3 error: err.message, 4 links: { 5 self: { 6 rel: 'self', 7 href: `${config.baseUrl}${req.originalUrl}`, 8 method: req.method as any 9 } 10 } 11 }); 12}
createRouter(__filename)
for all route files.__source
and __name
properties.createHATEOASMiddleware
to automatically generate HATEOAS links for your API.We welcome contributions! If you encounter bugs, have feature requests, or want to improve the library:
This project is licensed under the MIT License.
Every contribution matters, whether itβs bug fixes, feature requests, or improving documentation.
Fork and Clone the Repository
1git clone https://github.com/phamhung075/express-route-tracker.git 2cd express-route-tracker
Install Dependencies
1npm install
Make Changes
1git checkout -b feature/your-feature
1git add . 2git commit -m "Your detailed commit message"
For help or inquiries:
Happy Coding! πβ¨
No vulnerabilities found.
No security vulnerabilities found.