serve-static-corell v1.0.1
serve-static-corell
A lightweight static file server compatible with both Node.js and browser environments. This package bridges the gap between Node.js-specific modules (like events) and browser environments, ensuring compatibility and smooth operation in different contexts.
Features
- š„ļø Node.js Support: Serve static files in a Node.js environment with ease.
 - š Browser Support: Simulate static file serving in a browser environment.
 - š Environment Detection: Automatically detects whether it's running in Node.js or a browser and adapts.
 - ā
 Module Compatibility: Resolves Node.js-specific modules like 
eventsfor use in browsers. - ā” Lightweight: Minimal dependencies for maximum performance.
 
Installation
Install the package via npm:
npm install serve-static-corellUsage
In Node.js
Serve static files from a directory:
const serveStatic = require("serve-static-corell");
const http = require("http");
const path = require("path");
// Set up static file middleware
const staticMiddleware = serveStatic(path.join(__dirname, "public"));
// Create an HTTP server
const server = http.createServer((req, res) => {
  staticMiddleware(req, res);
});
// Start the server
server.listen(3000, () => {
  console.log("Server running at http://localhost:3000/");
});Place your static files (e.g., index.html, style.css, etc.) in the public directory. Access them via the browser at http://localhost:3000/.
In Browser
Simulate static file serving in a browser environment:
import serveStatic from "serve-static-corell";
// Map URLs to file content
const fileMap = {
  "/index.html": "<h1>Hello, World!</h1>",
  "/about.html": "<h1>About Us</h1>"
};
// Create a simulated static file middleware
const staticMiddleware = serveStatic(fileMap);
// Simulate a request
staticMiddleware({ url: "/index.html" }, (res) => {
  console.log(res.content); // Outputs: <h1>Hello, World!</h1>
});API
serveStatic(directoryOrFileMap)
Parameters:
directoryOrFileMap:- In Node.js: A directory path (e.g., 
path.join(__dirname, "public")). - In Browser: An object mapping URL paths to file content (e.g., 
{ "/index.html": "<h1>Hello</h1>" }). 
- In Node.js: A directory path (e.g., 
 
Returns:
A middleware function that serves static files.
Examples
Node.js Example
const serveStatic = require("serve-static-corell");
const http = require("http");
const path = require("path");
const staticMiddleware = serveStatic(path.join(__dirname, "public"));
const server = http.createServer((req, res) => {
  staticMiddleware(req, res);
});
server.listen(3000, () => {
  console.log("Server is running on http://localhost:3000/");
});Browser Example
import serveStatic from "serve-static-corell";
const fileMap = {
  "/index.html": "<h1>Welcome to the browser!</h1>",
  "/contact.html": "<h1>Contact Us</h1>"
};
const staticMiddleware = serveStatic(fileMap);
staticMiddleware({ url: "/contact.html" }, (res) => {
  console.log(res.content); // Outputs: <h1>Contact Us</h1>
});Development
Clone the Repository
To work on the package locally:
git clone https://github.com/yourusername/serve-static-corell.git
cd serve-static-corell
npm installBuild the Package
Use Webpack to build the package:
npm run buildRun Tests
Run tests using Node.js:
npm testHow It Works
Environment Detection:
- The package checks if it is running in a browser (
typeof window !== "undefined") or Node.js, and loads the appropriate implementation. 
- The package checks if it is running in a browser (
 Node.js Implementation:
- Uses the native 
fsmodule to read files from a directory and serve them via HTTP. 
- Uses the native 
 Browser Implementation:
- Simulates serving static files by mapping URLs to predefined file content.
 
Compatibility:
- The package includes a fallback for Node.js modules like 
events, allowing them to work seamlessly in the browser. 
- The package includes a fallback for Node.js modules like 
 
Directory Structure
serve-static-corell/
āāā src/
ā   āāā index.js             # Main entry point
ā   āāā browser/
ā   ā   āāā events.js        # Browser-compatible EventEmitter
ā   ā   āāā serveStatic.js   # Browser implementation
ā   āāā node/
ā       āāā events.js        # Node.js-compatible EventEmitter
ā       āāā serveStatic.js   # Node.js implementation
āāā dist/                    # Compiled outputs (CJS, ESM, UMD)
āāā tests/                   # Test files
āāā package.json             # Package metadata
āāā webpack.config.js        # Build configuration
āāā README.md                # Documentation
āāā LICENSE                  # License fileContributing
Contributions are welcome! Please follow these steps:
- Fork the repository.
 - Create a new branch for your feature or bug fix.
 - Make your changes and write tests if applicable.
 - Submit a pull request with a clear description of your changes.
 
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgements
This package is inspired by the need to bridge the gap between Node.js and browser environments when serving static files. Thanks to the open-source community for tools like Webpack and Babel that make such projects possible.