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.
- 🖥️ 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
events
for use in browsers. - ⚡ Lightweight: Minimal dependencies for maximum performance.
Install the package via npm:
npm install serve-static-corell
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/
.
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>
});
-
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.,
A middleware function that serves static files.
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/");
});
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>
});
To work on the package locally:
git clone https://github.com/yourusername/serve-static-corell.git
cd serve-static-corell
npm install
Use Webpack to build the package:
npm run build
Run tests using Node.js:
npm test
-
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
fs
module 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
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 file
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.
This project is licensed under the MIT License. See the LICENSE file for details.
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.