What is ecstatic?
The 'ecstatic' npm package is a simple static file server middleware for Node.js. It allows you to serve static files with ease, making it useful for development and simple web servers.
What are ecstatic's main functionalities?
Basic Static File Serving
This feature allows you to serve static files from a specified directory. In this example, files from the 'public' directory will be served on port 8080.
const http = require('http');
const ecstatic = require('ecstatic');
const server = http.createServer(
ecstatic({ root: __dirname + '/public' })
);
server.listen(8080);
Custom Error Pages
This feature allows you to handle errors and serve custom error pages. In this example, a custom 404 error page is served when a file is not found.
const http = require('http');
const ecstatic = require('ecstatic');
const server = http.createServer(
ecstatic({ root: __dirname + '/public', handleError: false })
);
server.on('request', (req, res) => {
res.on('error', (err) => {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>Custom 404 Page</h1>');
});
});
server.listen(8080);
Cache Control
This feature allows you to set cache control headers for the served files. In this example, the cache is set to expire in 3600 seconds (1 hour).
const http = require('http');
const ecstatic = require('ecstatic');
const server = http.createServer(
ecstatic({ root: __dirname + '/public', cache: 'max-age=3600' })
);
server.listen(8080);
Other packages similar to ecstatic
serve-static
'serve-static' is a middleware for serving static files in a Connect/Express application. It is similar to 'ecstatic' but is more commonly used in Express applications. It provides similar functionality with a focus on integration with Express.
http-server
'http-server' is a simple, zero-configuration command-line static HTTP server. It is similar to 'ecstatic' but is designed to be used as a standalone server rather than middleware. It is useful for quick static file serving without needing to write any code.
static-server
'static-server' is another simple static file server for Node.js. It is similar to 'ecstatic' but offers additional features like directory listings and custom headers. It is designed to be easy to use and configure.
Ecstatic
A simple static file server middleware that works with both Express and Flatiron
- Built-in simple directory listings
- Shows index.html files at directory roots when they exist
- Use it with a raw http server, express/connect, or flatiron/union!
Examples:
express
var express = require('express');
var ecstatic = require('ecstatic');
var app = express.createServer();
app.use(ecstatic(__dirname + '/public'));
app.listen(8080);
console.log('Listening on :8080');
union
var union = require('union');
var ecstatic = require('ecstatic');
union.createServer({
before: [
ecstatic(__dirname + '/public'),
]
}).listen(8080);
console.log('Listening on :8080');
flatiron
var union = require('union');
var flatiron = require('flatiron');
var ecstatic = require('ecstatic');
app = new flatiron.App();
app.use(flatiron.plugins.http);
app.http.before = [
ecstatic(__dirname + '/public')
];
app.start(8080);
console.log('Listening on :8080');
fall through
To allow fall through to your custom routes:
ecstatic(__dirname + '/public', {handleError: false})
API:
ecstatic(folder, opts={});
Pass ecstatic a folder, and it will return your middleware!
Turn on cache-control with opts.cache
, in seconds.
Turn off directory listings with opts.autoIndex === false
.
Turn on default file extensions with opts.defaultExt
. If opts.defaultExt
is true, it will default to html
. For example if you want a request to /a-file to server /root/a-file.html set this to true
. If you want to serve /root/a-file.json set opts.defaultExt
to json
.
middleware(req, res, next);
This works more or less as you'd expect.
ecstatic.showDir(folder);
This returns another middleware which will attempt to show a directory view. Turning on auto-indexing is roughly equivalent to adding this middleware after an ecstatic middleware with autoindexing disabled.
Tests:
npm test
License:
MIT/X11.