An open API service indexing awesome lists of open source software.

https://github.com/gera2ld/spa-static

A web server for SPAs.
https://github.com/gera2ld/spa-static

Last synced: about 2 months ago
JSON representation

A web server for SPAs.

Awesome Lists containing this project

README

          

# spa-static

![NPM](https://img.shields.io/npm/v/spa-static.svg)
![License](https://img.shields.io/npm/l/spa-static.svg)
![Downloads](https://img.shields.io/npm/dt/spa-static.svg)

A web server to serve static files for SPAs.

Requires Koa 2.

## Installation
``` sh
$ npm i spa-static
```

## Usage

### Shell command
``` sh
$ spa-static
# or
$ spa-static config.json

# Load from a yaml config file
$ spa-static config.yml
```

A sample `config.js` may be like this:

``` js
module.exports = [
{
host: '',
port: 4000,
staticDir: 'static',
},
];
```

Or `config.yml` like this:

``` yaml
- host: ''
port: 4000
staticDir: static
```

### Node.js command
``` js
require('spa-static')(config, (err, server) => {
if (err) throw err;
console.log(`Listening at ${server.address()}...`);
});
```

`config` is an object with following attributes:

* host

Default as `localhost`.

* port

Default as `4000`.

* prefix

Default as `''`.

* staticDir

Default as `./static`.

* index

Default as `/index.html`.

* headers

An object of headers to be set for each response.

### Koa middleware

``` js
const Koa = require('koa');
const spaStatic = require('spa-static/lib/middleware');

const app = new Koa();

app.use(spaStatic()); // use default settings
// or
app.use(spaStatic({
staticDir: 'some/other/path',
}));
```