Static file server
serveStatic
is a middleware that allows to send static files from specified directory.
import { serveStatic } from '@shelepuginivan/lunatic'import { join } from 'path'
const staticDir = join(__dirname, 'static')
app.use('/static', serveStatic(staticDir))
app.listen(8000)
In this example, all requests to /static
and /static/*
will be handled by
serveStatic
. It will send a file like as follows:
- Request path
/static/style.css
-> File<staticDir>/style.css
- Request path
/static/images/d430849b.png
-> File<staticDir>/images/d430849b.png
etc.
If requested file does not exist, it will respond with 404 Not Found
status.
serveStatic
is a configurable middleware, meaning that it accepts options
object:
import { serveStatic, ServeStaticOptions } from '@shelepuginivan/lunatic'import { join } from 'path'
const serveStaticOptions: ServeStaticOptions = { dotfiles: 'forbid', index: 'index.html', lastModified: false,}
app.use('/static', serveStatic(join(__dirname, 'static'), serveStaticOptions))
app.listen(8000)
Configuration options
dotfiles
How should the server respond when client attempts to access files starting with a dot (such as .htaccess
).
Accepts one of the following strings:
'allow'
— respond with200 OK
and send the file'forbid'
— respond with403 Forbidden
and don’t send the file'ignore'
— respond with404 Not Found
status and don’t send dotfile, i.e. pretend that the file does not exist
Default: 'ignore'
.
etag
Whether to enable
ETag
header. Accepts a boolean
.
Default: true
index
How should the server respond when client attempts to access a directory.
- Set to
string
value to send a specific filename that should be sent - Set to
false
to disable this feature. Server will respond with404 Not Found
Default: "index.html"
lastModified
Whether to enable
Last-Modified
header. Accepts a boolean
.
Default: true