# static-content

> HTTP Static Content Middleware

Latest version **1.2.2** (published 2024-06-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install static-content
pnpm add static-content
yarn add static-content
bun add static-content
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2024-06-25 |
| First published | 2014-06-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 134 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Alexander Kit |

## Links

- npm: https://www.npmjs.com/package/static-content
- Repository: https://github.com/atmajs/atma-server-static
- Homepage: https://github.com/atmajs/atma-server-static#readme
- Issues: https://github.com/atmajs/atma-server-static/issues
- npm.io page: https://npm.io/package/static-content

## Dependencies (3)

- [atma-io](https://npm.io/package/atma-io.md) ^1.2.52
- [atma-utils](https://npm.io/package/atma-utils.md) ^0.2.58
- [@types/node](https://npm.io/package/@types/node.md) ^14.14.41

## Recent versions

- 1.2.2 (latest) — 2024-06-25
- 1.2.1 — 2022-01-24
- 1.1.28 — 2020-04-10
- 1.1.27 — 2018-10-23
- 1.1.25 — 2016-03-02
- 1.1.24 — 2016-02-22
- 1.1.23 — 2015-07-05
- 1.0.22 — 2015-05-03
- 1.0.21 — 2014-08-09
- 1.0.20 — 2014-08-09
- 1.0.19 — 2014-08-08
- 1.0.18 — 2014-08-06
- 1.0.17 — 2014-08-05
- 1.0.16 — 2014-06-28
- 1.0.15 — 2014-06-27
- … 11 more at https://npm.io/package/static-content/versions

## README

Http Server static middleware
-----
[![Build Status](https://app.travis-ci.com/atmajs/atma-static-content.svg?branch=master)](https://app.travis-ci.com/github/atmajs/atma-static-content)
[![NPM version](https://badge.fury.io/js/static-content.svg)](http://badge.fury.io/js/static-content)
- Binary/String files _html|scripts|styles|texts|json|.etc_:
    - cache
    - gzip
    - File `read` middlewares. Refer to [atma-io](https://github.com/atmajs/atma-io) for documentation.
    - Virtual files
- Binary streams _images|audio|video|pdf|.etc_:
    - Range requests

- Best works with [atma-server](https://github.com/atmajs/atma-server)

#### API
```javascript
require('static-content'):StaticContentMiddleware;

StaticContentMiddleware {
    create: function(settings:StaticContentSettings): function(req, res, next, AppConfig),
    respond: function(req, res, next, AppConfig):null,
    send: ExpressSend,
    Cache: Object {
        status: function(enabled:Boolean)
        remove: function(path:String)
    }
}

StaticContentSettings {
    // Optional, static root folder
    base: String

    // Add or overwrite some mimeTypes
    mimeTypes: { MimeTypeString: Array<Extension> }
    extensions: MimeTypeString || Object {
        mimeType: MimeTypeString,

        // When utf8 is set, then file will be also cached and gzipped
        encoding: 'UTF-8' || null,

        // In seconds
        maxAge: Number
    },
    defaultMimeType: MimeTypeString,

    // Optional, set of default Headers
    headers: Object
}
AppConfig {
    // Static root folder resolving:
    // 1) Check `static` property in config
    // 2) Check `base` property in config
    // 3) otherwise, take CWD
    static: String
    base: String
}

ExpressSend = function(req, url, ExpressSettings)
ExpressSend {
    maxage: function(ms:Number): Self,
    pipe: function(res),

    // override handlers, support:
    // - `directory`
    // - `error`
    on: function(event:String, function(error:Object))
}

ExpressSettings {
    // base path
    root: String,
    // default `index.html` file
    index: String
}

```

#### Examples
##### NodeJS HTTP
```javascript
require('http')
    .createServer(require('static-content').create({ base: './content' }))
    .listen(5777);
```
##### Atma-Root-Application with ConnectJS
```javascript

atma
    .server
    .Application({
        configs: '/server/configs/*.yml'
    })
    .done(function(app){

        var server = connect()
            // dynamic content response
            .use(app.responder({
                // if dynamic endpoint is found from
                // Handlers | RESTful services | Pages
                // middleware pipeline will be run before calling the endpoint
                middleware: [
                    connect.query(),
                    connect.cookieParser(),
                    connect.bodyParser(),
                    connect.session({
                        // ..
                    }),
                    passport.initialize(),
                    passport.session()
                ]
            }))
            // Atma-Server static content middleware
            .use(require('static-content').create({
                base: './public/'
            }))
            .listen(5777);
    });
```
##### Atma-Sub-Application
```javascript
module.exports = atma
    .server
    .Application({})
    .done(function(app){
        app.responders([
            // dynamic content response
            app.responder({
                // if dynamic endpoint is found from
                // Handlers | RESTful services | Pages
                // middleware pipeline will be run before calling the endpoint
                middleware: [
                    connect.bodyParser(),
                    // ...
                ]
            }),

            // Atma-Server static content middleware
            require('static-content').respond
        ]);
    });
```

----
The MIT License

---
_Source: https://npm.io/package/static-content · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
