HTTP Static Content Middleware for Node.js
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.
Common errors
-
TypeError: require is not a function
cause Attempting to use `require()` in an ES Module context where it is not defined.fixIf 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'
cause The `base` directory configured for static content does not exist or the requested file is not found within it.fixVerify 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.
Warnings
- gotcha This 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.
- gotcha The `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.
- gotcha Advanced 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`.
Install
-
npm install static-content -
yarn add static-content -
pnpm add static-content
Imports
- StaticContentMiddleware
import StaticContentMiddleware from 'static-content';
const StaticContentMiddleware = require('static-content'); - create
import { create } from 'static-content';const createStaticMiddleware = require('static-content').create; - Cache
import { Cache } from 'static-content';const StaticCache = require('static-content').Cache;
Quickstart
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');
});