How diegofmo0802/servercore works
Project type and purpose
• This is a Node.js/TypeScript library—“ServerCore”—that provides a lightweight framework for building HTTP/HTTPS servers, pluggable routing rules, WebSocket handling, templating, session/cookie management, plus optional “beta” modules for JWT and Mail.
• It is not a full-stack app but a server‐framework/toolkit intended for embedding in other projects.
File structure
• Top‐level:
• package.json, tsconfig.json, README.md, LICENSE
• global/ (static templates & CSS for the custom HSaml template engine)
• src/ (all TypeScript source code)
• tests/ & examples/
• src/ splits into:
• Beta/ (JwtManager.ts, Mail.ts — optional sub‐modules)
• Server/ (HTTP & HTTPS “Server.ts”, routing rules, Request/Response, BodyParser, Cookie, Session, WebSocket subfolder)
• Core files: ServerCore.ts (entry point), Template.ts (HSaml engine), ConsoleUI.ts, Debug.ts, Config.ts
• Utilities/ (Env, Path, general helper functions)
README for additional insights
• Emphasizes four routing‐rule types (Folder, File, Action, WebSocket) managed by ServerCore.Rule
• Shows how HTTP/HTTPS is initialized, how to register routes, and how to import Beta modules
• Mentions evolving template engine and future split of Beta modules
a. Main components
1. Client Layer
• HTTP/HTTPS clients (browsers, REST clients)
• WebSocket clients
2. ServerCore Library
• Entry: ServerCore class (src/ServerCore.ts)
• Underlying servers: Node HTTP.Server & HTTPS.Server (src/Server/Server.ts)
• Routing & Rule Engine (src/Server/Rule.ts)
• Request & Response abstractions (src/Server/Request.ts, Response.ts)
• Parsers & middleware:
– BodyParser (BodyParser.ts)
– Cookie manager (Cookie.ts)
– Session manager (Session.ts)
• WebSocket support (src/Server/WebSocket/WebSocket.ts + Chunk.ts)
• Template engine: Template.ts & global/Style + global/Template (custom HSaml)
• Beta namespace: JwtManager.ts, Mail.ts (optional pluggable modules)
• Utilities & config: Utilities/*.ts + Config.ts + Debug.ts + ConsoleUI.ts
3. External resources
• File system (static Folder/File handlers read from disk)
• SSL certificate files (for HTTPS)
• (Optional) SMTP server for Mail, Identity provider for JWT
b. Relationships and interactions
• ServerCore instantiates HTTP and/or HTTPS server listeners
• Incoming HTTP request → Node HTTP.Server emits → wrapped in custom Request object → middleware stack:
1. BodyParser
2. Cookie parser
3. Session loader
• Request passed to Rule Engine which matches against defined Rules:
• Folder/File → serves static content from disk
• Action → invokes user callback(request, response)
• WebSocket → upgrades to WS connection, hands off to WebSocket handler
• Response object sends data back over HTTP
• Template engine invoked by Action handlers or static rendering
• Beta.JwtManager and Beta.Mail injected or imported as needed by user code via ServerCore.Beta
c. Architectural patterns & design principles
• Modular architecture: core + optional Beta modules
• Rule‐based routing (pattern matching with wildcards and parameter extraction)
• Middleware‐style parsing pipeline (body, cookies, sessions)
• Adapter pattern: wrapping Node’s HTTP.Server in a higher‐level ServerCore API
• Extension points: addRule/addFolder/addFile/addAction/addWebSocket
d. Technologies, frameworks, and libraries
• Node.js built‐in http, https, fs, events
• TypeScript for source
• Custom HSaml template engine
• WebSocket protocol handling in src/Server/WebSocket
Key points to capture
• How ServerCore wraps Node’s HTTP/HTTPS primitives
• Detail the middleware → routing → handler flow
• Four rule types and WebSocket upgrade path
• Where template rendering and Beta modules fit in
• Do not omit configuration, utilities, and debug paths