Now an http server is listening on port 8000 with the content of local directory /public being served at http://localhost:8000/.

But beyond serving static files it will automatically render templates when requested, so the above example will map the local file structure like below.

/public/index.html as http://localhost:8000/index.html
/public/images/my.png as http://localhost:8000/images/my.png
----
/public/about.jade as http://localhost:8000/about.html
/public/style/my.styl as http://localhost:8000/style/my.css

index.html and images/my.png are served as-is, but about.jade and style/my.styl are served as their rendered content - all automatically because of the engines added above.

Rendering engines

Currently Jade and Stylus rendering engines are available. Also a minifier engine is available.

See section "Customizing" below on how to implement your own engines.

Caching

The build-in default of Smart Static is to just re-render the templates whenever they are requested. This might work with small websites with relative small amounts of users, but rendering can be a cumbersome task - so caching them is a good idea.

See the "Customizing" section below on how to implement your own cache plug-ins.

Options

Smart Static supports some options when creating - some of them you've already seen practiced above - more specifically the engines and cache option.

Currently these options are available.

Option

Type

Dafault

Description

engine

Array

None

An array of template engines.

cache

Object

None

A cache plug-in to provide caching

etag

Boolean

true

Use Etag for HTTP cache control

lastModified

Boolean

true

Send Last-Modified header. Uses file or template modification date.

index

Array

['index.html']

An array of file names to be tested for and used - in prefered order - when directories are requested.

serveDir

String

'/'

Serve files from a subdirectory.

allowHidden

Boolean

false

Allow acces to hidden (dot) files

allowTemplates

Boolean

false

Allow access to template files

accessControl

Object

None

Specify access (see section Access Control)

Access Control

Besides the allowHidden and allowTemplates options, Smart Static also supports more fine-grained control through the accessControl option.

An example below.

{
accessControl: {
deny: [
'.ext',
/^.*?\.ext2$/
]
}
}

The above example denies access to files with the .ext extension or with filenames that match the regular expression ^.?\.ext2$ - all other files are allowed. If you replace deny with allow it turns around - allowing only the files specified and denying all others.

The array can contain strings which match file extensions, or regular expressions which are matched against the entire filename.