Skip to content
This repository

A simple static file server middleware that works with core http, Express and Flatiron

branch: master

Merge release 0.5.1

Was trying to publish v0.5.0 on detached HEAD when nodjatso messed up
and my package only half-published. Because you can't do force
publishes to fix these kind of things (thanks dogmatists) I had to do
a version bump.

latest commit ec265505ac
Joshua Holbrook authored
Octocat-spinner-32 example [minor] Updates to examples October 21, 2013
Octocat-spinner-32 lib Fixes TypeError: Cannot read property 'range' of undefined February 09, 2014
Octocat-spinner-32 test Fallback to UTF-8 encoding in case when mime.charsets.lookup returns … December 09, 2013
Octocat-spinner-32 .gitignore [dist] Added .gitignore November 10, 2012
Octocat-spinner-32 .travis.yml [travis] Test 0.10, don't test 0.6 August 16, 2013
Octocat-spinner-32 Rename to April 04, 2014
Octocat-spinner-32 LICENSE.txt Add license file October 08, 2013
Octocat-spinner-32 Rename to April 04, 2014
Octocat-spinner-32 package.json Release 0.5.1 April 07, 2014

Ecstatic build status

A simple static file server middleware. Use it with a raw http server, express/connect, or flatiron/union!


express 3.0.x

var http = require('http');
var express = require('express');
var ecstatic = require('ecstatic');

var app = express();
app.use(ecstatic({ root: __dirname + '/public' }));

console.log('Listening on :8080');


var union = require('union');
var ecstatic = require('ecstatic');

  before: [
    ecstatic({ root: __dirname + '/public' }),

console.log('Listening on :8080');

stock http server

var http = require('http');
var ecstatic = require('ecstatic');

  ecstatic({ root: __dirname + '/public' })

console.log('Listening on :8080');

fall through

To allow fall through to your custom routes:

ecstatic({ root: __dirname + '/public', handleError: false })



Pass ecstatic an options hash, and it will return your middleware!

var opts = {
             root          : __dirname + '/public',
             baseDir       : '/',
             cache         : 3600,
             showDir       : false,
             autoIndex     : false,
             humanReadable : true,
             si            : false,
             defaultExt    : 'html',
             gzip          : false

If opts is a string, the string is assigned to the root folder and all other options are set to their defaults.


opts.root is the directory you want to serve up.


opts.baseDir is / by default, but can be changed to allow your static files to be served off a specific route. For example, if opts.baseDir === "blog" and opts.root = "./public", requests for localhost:8080/blog/index.html will resolve to ./public/index.html.


Customize cache control with opts.cache , if it is a number then it will set max-age in seconds. Other wise it will pass through directly to cache-control. Time defaults to 3600 s (ie, 1 hour).


Turn on directory listings with opts.showDir === true. Defaults to false.


If showDir is enabled, add human-readable file sizes. Defaults to true. Aliases are humanreadable and human-readable.

If showDir and humanReadable are enabled, print file sizes with base 1000 instead of base 1024. Name is inferred from cli options for ls. Aliased to index, the equivalent option in Apache.


Serve /path/index.html when /path/ is requested. Turn off autoIndexing with opts.autoIndex === false. Defaults to true.


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 resolve to ./public/a-file.html, set this to true. If you want /a-file to resolve to ./public/a-file.json instead, set opts.defaultExt to json.


Set opts.gzip === true in order to turn on "gzip mode," wherein ecstatic will serve ./public/some-file.js.gz in place of ./public/some-file.js when the gzipped version exists and ecstatic determines that the behavior is appropriate.


Turn off handleErrors to allow fall-through with opts.handleError === false, Defaults to true.

middleware(req, res, next);

This works more or less as you'd expect.


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.

ecstatic command

to start a standalone static http server, run npm install -g ecstatic and then run ecstatic [dir?] [options] --port PORT all options work as above, passed in optimist style. port defaults to 8000. If a dir or --root dir argument is not passed, ecsatic will serve the current dir.


Ecstatic has a fairly extensive test suite. You can run it with:

$ npm test


Without outside contributions, ecstatic would wither and die! Before contributing, take a quick look at the contributing guidelines in ./ . They're relatively painless, I promise.



Something went wrong with that request. Please try again.