2.5.0 • Published 4 months ago

servatron v2.5.0

Weekly downloads
29
License
MIT
Repository
github
Last release
4 months ago

servatron

An extremely lightweight static file server that can be used as a handler or cli.

Installation

npm install --save servatron

(or) globally:

npm install -g servatron

Usage

CLI

servatron --directory dist --port 8000 --spa

Optional arguments are:

--directory (-d) pathName      specify a directory to server the files from (can provider multiple)
--bind (-b) hostname           what host to bind on (default: 0.0.0.0)
--port (-p) number             what port to listen on (default: 8000)
--spa                          when a path is not found, deliver the index file
--spa-index                    what name spa mode should look for the html file (default: index.html)
--http2                        use http2 as the server protocol
--key                          what key to use for http2
--cert                         what cert to use for http2
--ca                           optionally add a ca for http2
--anti-cors                    set all CORS headers to the most flexible

Code - HTTP 1

const http = require('http');
const servatron = require('servatron/http');

const staticHandler = servatron({
  directory: './dist',
  spa: true,
  spaIndex: 'index.html'
})

// Use only the staticHandler
http.createServer(staticHandler).listen(8000)

// (or) Mix custom handler logic with staticHandler
http.createServer(function (request, response) {
  if (request.url.startsWith('/api')) {
    response.end('this could be an api')
    return
  }

  staticHandler(request, response)
).listen(8000, '0.0.0.0')

Code - HTTP 2

const http2 = require('http2');
const servatron = require('servatron/http2');

const server = http2.createSecureServer({
  key: fs.readFileSync('./key.pem'),
  cert: fs.readFileSync('./cert.pem')
});
server.on('error', (error) => console.error(error));

// Use only the staticHandler
server.on('stream', staticHandler);

server.listen(8000, '0.0.0.0');

// (or) Mix custom handler logic with staticHandler
server.on('stream', function (stream, headers) {
  if (headers[':path'].startsWith('/api')) {
    stream.end('this could be an api');
    return;
  }

  staticHandler(stream, header);
});

server.listen(8000, '0.0.0.0');
2.5.0

4 months ago

2.4.3

1 year ago

2.4.1

2 years ago

2.4.0

2 years ago

2.4.2

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

2.3.0

2 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.8

3 years ago

2.1.7

3 years ago

2.1.6

3 years ago

2.1.4

3 years ago

2.1.5

3 years ago

2.1.3

3 years ago

2.1.2

3 years ago

2.0.3

3 years ago

2.1.1

3 years ago

2.0.4

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.3

3 years ago

1.0.0

3 years ago