Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
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. Use it with a raw http server, express/connect, or flatiron/union!
var http = require('http');
var express = require('express');
var ecstatic = require('ecstatic');
var app = express();
app.use(ecstatic({ root: __dirname + '/public' }));
http.createServer(app).listen(8080);
console.log('Listening on :8080');
var union = require('union');
var ecstatic = require('ecstatic');
union.createServer({
before: [
ecstatic({ root: __dirname + '/public' }),
]
}).listen(8080);
console.log('Listening on :8080');
var http = require('http');
var ecstatic = require('ecstatic');
http.createServer(
ecstatic({ root: __dirname + '/public' })
).listen(8080);
console.log('Listening on :8080');
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,
defaultExt : 'html',
gzip : false
}
opts.root
opts.root
is the directory you want to serve up.
opts.baseDir
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
.
opts.cache
Customize cache control with opts.cache
, in seconds. Time defaults to 3600 s
(ie, 1 hour).
opts.showDir
Turn on directory listings with opts.showDir === true
.
opts.autoIndex
Turn on autoIndexing with opts.autoIndex === true
. Defaults to false.
opts.defaultExt
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
.
opts.gzip
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.
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.
npm test
MIT/X11.
FAQs
A simple static file server middleware
The npm package ecstatic receives a total of 134,105 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.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.