Node-Static
`node-static` is a legacy module designed for serving static files over HTTP, adhering to RFC 2616, including support for conditional GET and HEAD requests. The last published version, 0.7.11, dates back over eight years, targeting very old Node.js environments (engine requirement `node >= 0.4.1`). It simplifies static file serving by abstracting `Cache-Control` headers and basic error handling, allowing developers to specify a root directory and cache duration. Due to its age and lack of maintenance, it is now considered abandoned. Its API heavily relies on callback patterns and older Node.js `http` module stream handling, which are not idiomatic in modern JavaScript development, making it unsuitable for contemporary projects.
Common errors
-
Error: Cannot find module 'node-static'
cause The package is not installed or the `require` path is incorrect.fixRun `npm install node-static` in your project directory. Ensure `require('node-static')` is correctly spelled and located. -
TypeError: request.addListener is not a function
cause This indicates a mismatch between the expected `request` object API in `node-static` and the `http.IncomingMessage` object in newer Node.js versions, where `addListener` might not behave as expected with the `resume()` call.fixThis problem suggests fundamental incompatibility with modern Node.js stream handling. The most robust fix is to replace `node-static` with an actively maintained static file serving solution compatible with your Node.js version. -
ReferenceError: static is not defined
cause The `static` variable was not properly assigned or is out of scope. This often happens if `require('node-static')` is omitted or placed incorrectly.fixEnsure `const static = require('node-static');` is present and correctly scoped at the top of your file before `static.Server` is used. -
Error [ERR_REQUIRE_ESM]: require() of ES Module [...] not supported.
cause You are attempting to use `require()` to load `node-static` in an ES Module context, or vice-versa with a modern dependency. `node-static` is CommonJS-only.fixIf your project is ESM, you cannot directly `require` `node-static`. Consider migrating your project to CommonJS if `node-static` is critical, or, more practically, replace `node-static` with an ESM-compatible static file server.
Warnings
- breaking Outdated Node.js `http` stream API usage: The `request.addListener('end').resume()` pattern is an older method for handling readable streams. In modern Node.js, request streams are typically flowing by default, and this pattern may cause unexpected behavior or hangs.
- breaking Lack of ESM support: `node-static` is a CommonJS-only package. Attempting to use `import` statements in an ESM context will result in an `ERR_REQUIRE_ESM` error.
- gotcha Abandoned Status & Security Risks: The package has not been updated in over eight years. This means it likely contains unpatched security vulnerabilities and is not compatible with modern Node.js features or best practices, posing a significant security risk for production applications.
- gotcha Manual Error Handling: Unlike modern web frameworks that integrate error middleware, `node-static` requires explicit `response.writeHead` and `response.end` calls within its error callback. This increases boilerplate and potential for inconsistencies in error responses.
- deprecated Outdated Node.js Compatibility: The package specifies Node.js `>= 0.4.1`, a version that reached End-of-Life over a decade ago. Using `node-static` with modern Node.js versions may lead to unexpected behavior due to API changes and deprecations.
Install
-
npm install node-static -
yarn add node-static -
pnpm add node-static
Imports
- static.Server
import { Server } from 'node-static';const static = require('node-static'); const fileServer = new static.Server('./public'); - fileServer.serve
fileServer.serve(request, response, next);
fileServer.serve(request, response);
- fileServer.serveFile
fileServer.serveFile('error.html', 404, request, response);fileServer.serveFile('/error.html', 404, {}, request, response);
Quickstart
var static = require('node-static');
var http = require('http');
// Create a node-static server instance to serve the './public' folder.
// Ensure a 'public' directory exists in your project root with some static files.
var file = new static.Server('./public', {
cache: 3600, // Cache files for 1 hour (default)
headers: {
'X-Powered-By': 'node-static'
}
});
http.createServer(function (request, response) {
// This pattern for handling request 'end' and 'resume' is for older Node.js versions.
request.addListener('end', function () {
file.serve(request, response, function (err, result) {
if (err) {
console.error("Error serving " + request.url + " - " + err.message);
// Respond manually to the client if an error occurs
response.writeHead(err.status || 500, err.headers || { 'Content-Type': 'text/plain' });
response.end("Error serving file: " + err.message);
}
});
}).resume(); // Important for older Node.js to ensure stream is read
}).listen(8080, function() {
console.log('node-static server running on http://localhost:8080');
console.log('Serving files from ./public');
});