Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Thumbs.db
.DS_Store?
/.idea/
/node_modules/
.project
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "iojs"
- "6"
- "7"
- "8"
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,50 @@ server.start();
guarantee that it will be executed. Position specifier only affect the placement
of your middleware in the Connect's stack.

## Command line server

Install globally via `npm`:

npm install spa-server -g

This will install `spa-server` globally so that it can be run from the command line.

### Usage

spa-server [options] [path]

### Available Options

`-a` Listen address. Defaults to 0.0.0.0

`-d` Show dotfiles. Defaults to `ignore`

`-f` Fallback URL. Example:

spa-server -f "/index.html" .

`-F` Fallback handler function or object. Example:

spa-server -F "({'*': '/index.html'})" .


`-h` or `-?` Show help

`-l` Log format. Defaults to `combined`. See [available formats](https://github.com/expressjs/morgan#predefined-formats)

`-m` Middleware

`-p` Listen port. Defaults to 8888.

`-q` Quiet mode. Disables logging.

`-s` Servestatic config object. See [Servestatic options](https://github.com/expressjs/serve-static#options)

# Set cache-control max-age
spa-server -s "({maxAge: 10000})" .
# Set CORS header
spa-server -s "({setHeaders: ((res, path) => {res.setHeader('Access-Control-Allow-Origin', '*')})})" .


## Changelog

Expand Down
64 changes: 64 additions & 0 deletions bin/spa-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env node

'use strict';

var morgan = require('morgan'),
serverFactory = require('../lib/server'),
argv = require('yargs').argv,
extend = require('xtend'),
path = argv._[0],
port = argv.p || 8888,
address = argv.a || '0.0.0.0',
fallbackURL = argv.f,
help = argv.h || argv['?'],
quiet = argv.q ,
dotfiles = argv.d || 'ignore',
logFormat = argv.l || 'combined',
fallbackHandler = eval(argv.F),
userServeStaticConfig = eval(argv.s),
userMiddlewares = eval(argv.m || []),
defaultServeStaticConfig = {dotfiles: dotfiles},
middleware = [].concat(userMiddlewares),
serveStaticConfig = extend({}, defaultServeStaticConfig, userServeStaticConfig);

if (help || !path) {
console.log([
'usage: spa-server [options] [path]',
'',
'options:',
' -a Listen address [0.0.0.0]',
' -d Show dotfiles [ignore]',
' -f Fallback URL',
' -F Fallback handler function or object',
' -h -? Print this message and exit.',
' -l Log format [combined]',
' -m Middleware',
' -p Listen port [8888]',
' -q Disable logging',
' -s Servestatic config'
].join('\n'));
process.exit();
}

if (fallbackURL && fallbackHandler) {
console.log('Error: Fallback URL (-f) and handler (-F) are mutually exclusive!');
process.exit(1);
}

if (!quiet) {
middleware.push({
middleware: morgan(logFormat),
before: '$start'
});
}

var server = serverFactory.create({
path: path,
port: port,
fallback: fallbackURL || fallbackHandler,
serveStaticConfig: serveStaticConfig,
hostname: address,
middleware: middleware
});

server.start();
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
"repository": "betsol/spa-server",
"license": "MIT",
"dependencies": {
"connect": "^3.3.5",
"mime": "^1.3.4",
"serve-static": "^1.9.2",
"xtend": "^4.0.0"
"connect": "^3.6.5",
"mime": "^1.4.1",
"serve-static": "^1.13.1",
"xtend": "^4.0.1",
"yargs": "^10.0.3",
"morgan": "^1.9.0"
},
"devDependencies": {
"cheerio": "^0.19.0",
Expand All @@ -31,9 +33,13 @@
"lib/",
"README.md",
"changelog.md",
"index.js"
"index.js",
"bin/"
],
"scripts": {
"test": "make test"
},
"bin": {
"spa-server": "./bin/spa-server"
}
}