serve-static-ng
v0.0.1
Published
Serve files
Downloads
158
Readme
serve-static-ng
Static file serving middleware for Node.js, based on https://www.npmjs.com/package/serve-static.
It provides the same API and options as serve-static, with one additional feature:
Custom File System support (
fs)
Features
- Static file serving
- Express / Router compatible middleware
- Cache, ETag, range requests
- Directory index + redirects
- NEW: pluggable filesystem (fs abstraction)
Installation
npm install serve-static-ngBasic usage
const express = require('express')
const serveStatic = require('serve-static-ng')
const app = express()
app.use(serveStatic('public'))
app.listen(3000)Custom File System (memfs example)
This example uses an in-memory filesystem with a hardcoded index.html.
Install memfs
npm install memfsExample
const express = require('express')
const { createFsFromVolume, Volume } = require('memfs')
const serveStatic = require('serve-static-ng')
const app = express()
// Create in-memory filesystem
const vol = Volume.fromJSON({
'/index.html': '<h1>Hello from memfs 🚀</h1>'
})
const memfs = createFsFromVolume(vol)
// Serve static using custom fs
app.use(serveStatic('/', {
fs: memfs
}))
app.listen(3000)Now visiting /index.html will serve:
<h1>Hello from memfs 🚀</h1>Required FS interface
Your custom filesystem must implement:
- fs.stat(path, cb)
- fs.createReadStream(path, options)
Options
All options from serve-static are supported:
- maxAge
- etag
- lastModified
- index
- redirect
- fallthrough
- setHeaders
- dotfiles
- extensions
- immutable
- cacheControl
- acceptRanges
- root
Differences vs serve-static
Added
options.fs: custom filesystem backend
Default
- Uses Node.js
fswhen no custom FS is provided
Use cases
- In-memory FS (memfs)
- Virtual filesystems
- Testing environments
- Remote storage adapters (S3, GCS, etc.)
License
MIT
