serve-static-corell

1.0.1 • Public • Published

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 events for use in browsers.
  • Lightweight: Minimal dependencies for maximum performance.

Installation

Install the package via npm:

npm install serve-static-corell

Usage

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>" }).

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 install

Build the Package

Use Webpack to build the package:

npm run build

Run Tests

Run tests using Node.js:

npm test

How It Works

  1. Environment Detection:

    • The package checks if it is running in a browser (typeof window !== "undefined") or Node.js, and loads the appropriate implementation.
  2. Node.js Implementation:

    • Uses the native fs module to read files from a directory and serve them via HTTP.
  3. Browser Implementation:

    • Simulates serving static files by mapping URLs to predefined file content.
  4. Compatibility:

    • The package includes a fallback for Node.js modules like events, allowing them to work seamlessly in the browser.

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 file

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make your changes and write tests if applicable.
  4. 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.


Package Sidebar

Install

npm i serve-static-corell

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

13.2 kB

Total Files

12

Last publish

Collaborators

  • yesheng80