Express Server Routing
Relevant source files
The following files were used as context for generating this wiki page:
The Express server routing system serves as the backbone of the BV-BRC (PATRIC) web application, managing how HTTP requests are directed to specific handlers, middleware, and view engines. It employs a modular architecture where the main application file delegates path-specific logic to dedicated route modules. This system handles a wide variety of responsibilities, including serving static assets, managing user authentication flows, interfacing with the workspace service, and rendering EJS templates for the front-end.
Sources: app.js:1-115, routes/index.js:1-105
Server Architecture and Initialization
The routing lifecycle begins in app.js, where the Express application is initialized. The server configures global middleware for logging, cookie parsing, and favicon handling before mounting specialized routers. A significant portion of the routing logic involves setting up proxies to external services and defining static directories for JavaScript libraries and stylesheets.
Global Middleware and Proxy Configuration
The server utilizes express-http-proxy and http-proxy to redirect specific local paths to remote service URLs defined in the system configuration. This allows the web application to act as a unified gateway for various backend microservices.
The diagram above illustrates the high-level request flow from the initial entry point to the final response.
Sources: app.js:35-65, app.js:100-115
Router Mounting
The application distributes routing logic across several files located in the routes/ directory. These are mounted to specific base paths:
| Base Path | Router Module | Purpose |
|---|---|---|
/ | routes/index | Home, about, and informational pages |
/workspace | routes/workspace | Workspace navigation and data management |
/users | routes/users | User-related resources and listings |
/view | routes/viewers | Specialized data visualization routes |
/app | routes/apps | Service-specific application routes |
Sources: app.js:125-155, routes/workspace.js:4-11, routes/users.js:4-7
Primary Route Modules
Informational and Static Pages (routes/index.js)
The index router handles general navigation. For most informational pages, it sets the req.applicationModule to p3/app/p3app and renders specific EJS templates located in the views/pages/ directory.
Key endpoints include:
- Home (
/): Renders the main portal index. - Documentation: Paths like
/docs/user-guides/sars-cov-2provide access to specific scientific tutorials. - Outreach: Includes
/brc-calendar,/contact, and/funding.
Sources: routes/index.js:4-100
Workspace and Data Management (routes/workspace.js)
The workspace router uses a wildcard pattern (*) to capture all sub-paths. It implements strict cache-control headers to ensure that workspace data remains private and up-to-date, preventing browsers from storing sensitive data locally.
router.get('*', function (req, res) {
req.applicationModule = 'p3/app/p3app';
res.set('cache-control', 'private, no-store, max-age=0, no-cache, must-revalidate, post-check=0, pre-check=0');
res.set('pragma', 'no-cache');
res.set('expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
res.render('p3app', { title: 'PATRIC Workspace', request: req, response: res });
});
Sources: routes/workspace.js:4-11
Client-Side Routing Integration
While Express handles the initial request and document delivery, the project uses a custom Dojo-based router (public/js/p3/router.js) to handle Single Page Application (SPA) navigation. This client-side router intercepts URL changes and updates the view without full page reloads.
Router Mechanics
The client router extends dojo/router/RouterBase and maintains a queue of callbacks for registered regex patterns. When a path change occurs via window.history.pushState or popstate, the router executes the matched callback.
The sequence diagram shows how client-side routing avoids unnecessary server round-trips for internal navigation.
Sources: public/js/p3/router.js:45-125, public/js/p3/app/p3app.js:150-250
Configuration and Environment
The routing behavior is influenced by environment variables and system configuration.
- Production vs. Development: In development, error handlers provide full stack traces, while production hides implementation details from the user.
- App Versioning: Static asset routes are dynamically constructed using the version specified in
package.json(e.g.,/js/[version]/). - Authorization: Many routes check for
window.App.authorizationTokenbefore allowing navigation to sensitive areas like/job/or/uploads/.
Sources: app.js:1-25, app.js:110-120, app.js:175-195, public/js/p3/app/p3app.js:200-215
Conclusion
Express server routing in this project is a hybrid model. The server handles initial entry points, static file distribution, and security headers, while delegating the complex UI state management to a sophisticated client-side routing system. This dual approach ensures high performance for content delivery while maintaining the interactive feel of a modern web application.
Sources: app.js, public/js/p3/app/p3app.js