Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
A simple static file server middleware that works with both Express and Flatiron
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.
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);
'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' 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' 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.
A simple static file server middleware that works with both Express and Flatiron
var express = require('express');
var ecstatic = require('../')(__dirname + '/public');
var app = express.createServer();
app.use(ecstatic);
app.listen(8080);
console.log('Listening on :8080');
var union = require('union');
var ecstatic = require('../')(__dirname + '/public');
union.createServer({
before: [
ecstatic
]
}).listen(8080);
console.log('Listening on :8080');
Pass ecstatic a folder, and it will return your middleware!
This works more or less as you'd expect.
npm test
This project's implementation is pretty much g2g (thanks @substack) but currently does not work in Union. This is because Union's response piping is currently broken.
Once Union is fixed, this will work there as well.
MIT/X11.
FAQs
A simple static file server middleware
The npm package ecstatic receives a total of 653,617 weekly downloads. As such, ecstatic popularity was classified as popular.
We found that ecstatic demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.