Registry / http-networking / static-content

static-content

JSON →
library1.2.2jsnpmunverified

This middleware is part of the `atmajs` ecosystem, designed for handling static file serving in Node.js HTTP servers. It provides features like caching, gzipping, and range requests for optimized delivery of various content types including HTML, scripts, images, and videos. It supports custom MIME types, encoding configurations, and integrates seamlessly with `atma-server` and Connect-style middleware stacks. Currently at version 1.2.2, its release cadence is not explicitly defined, but it appears to be a stable, mature component within its specific framework, primarily targeting CommonJS environments. Key differentiators include its tight integration with `atma-io` for advanced file reading and its support for virtual files and custom file `read` middlewares, offering more flexibility than generic static servers.

npm install static-content
INSTALL
IMPORT
SIG · STATIC-CONTENT
S
static-content
http-networkingjavascriptv1.2.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

StaticContentMiddleware
const StaticContentMiddleware = require('static-content');
import StaticContentMiddleware from 'static-content';
The package is primarily designed for CommonJS. Direct ESM import might require bundler configuration or a CJS wrapper.
create
const createStaticMiddleware = require('static-content').create;
import { create } from 'static-content';
The `create` function is the primary API for initializing the middleware. It's accessed as a property of the main export.
Cache
const StaticCache = require('static-content').Cache;
import { Cache } from 'static-content';
The `Cache` object provides utilities for managing the static content cache, such as checking status and removing specific entries. Accessed as a property of the main CommonJS export.

Demonstrates setting up a basic Node.js HTTP server to serve static files from a 'public' directory, including custom MIME types and caching.

const http = require('http'); const staticContent = require('static-content'); // Create a static content middleware instance const staticMiddleware = staticContent.create({ base: './public', mimeTypes: { 'application/javascript': ['mjs'] }, extensions: { 'text/html': { encoding: 'UTF-8', maxAge: 3600 // Cache for 1 hour } } }); // Create a simple HTTP server using the middleware http.createServer((req, res) => { // The middleware handles static content requests staticMiddleware(req, res, () => { // If static content is not found, handle dynamic content or return 404 res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('404 Not Found'); }); }).listen(5777, () => { console.log('Static server running on http://localhost:5777'); console.log('Serving files from ./public'); });
Debug
Known issues
gotchaThis package is primarily designed for CommonJS (CJS) environments, using `require()`. Direct ES Module (ESM) imports may not work out of the box and might require a bundler or a CJS wrapper.
fix
For ESM projects, consider using a CommonJS compatibility layer or transpiling your code. If possible, stick to `require()` or use a different static serving middleware if ESM is a strict requirement.
affects: All versions
gotchaThe `base` static root folder can be resolved from `AppConfig.static` or `AppConfig.base` if available from the `atma-server` context, otherwise it defaults to the Current Working Directory (CWD). This implicit behavior can lead to unexpected file resolution if not explicitly set in `create()` settings.
fix
Always explicitly define the `base` option in the `staticContent.create({ base: './your-static-folder' })` settings to ensure predictable file serving paths.
affects: All versions
gotchaAdvanced file reading features, such as custom file `read` middlewares, are powered by `atma-io`. While `static-content` can operate independently, leveraging these features requires understanding and potentially installing `atma-io`.
fix
Refer to the `atma-io` documentation for details on extending file reading capabilities. Install `atma-io` if you plan to implement custom file processing pipelines.
affects: All versions
Errors
Common errors & fixes
TypeError: require is not a function
Attempting to use `require()` in an ES Module context where it is not defined.
fix
If your project is ESM, ensure your import statements are correctly configured for CommonJS modules, or use a tool like Rollup/Webpack to bundle. Consider using `createRequire` from the `module` built-in module for specific CJS imports in ESM.
Error: ENOENT: no such file or directory, stat './public/index.html'
The `base` directory configured for static content does not exist or the requested file is not found within it.
fix
Verify that the `base` path provided in `staticContent.create({ base: '...' })` is correct and relative to your application's execution directory, or an absolute path. Ensure the static files you are trying to serve actually exist in that location.
Upgrade
Version history
1.2.2latest on npm
Audit
Dependencies
atma-iooptionalUsed for advanced file reading middlewares and virtual files.
atma-serveroptionalBest works with and provides configuration context for the middleware.
connectoptionalUsed in example configurations, indicating compatibility with Connect/Express-like middleware pipelines.
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
static-content — npm install static-content · libregistry