Skip to content

Commit 15bf09c

Browse files
committed
Added CLI frontend
1 parent c6643b3 commit 15bf09c

File tree

4 files changed

+117
-5
lines changed

4 files changed

+117
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Thumbs.db
33
.DS_Store?
44
/.idea/
55
/node_modules/
6+
.project

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,48 @@ server.start();
328328
guarantee that it will be executed. Position specifier only affect the placement
329329
of your middleware in the Connect's stack.
330330

331+
## Command line server
332+
333+
Install globally via `npm`:
334+
335+
npm install spa-server -g
336+
337+
This will install `spa-server` globally so that it can be run from the command line.
338+
339+
### Usage
340+
341+
spa-server [options] [path]
342+
343+
### Available Options
344+
345+
`-a` Listen address. Defaults to 0.0.0.0
346+
347+
`-d` Show dotfiles. Defaults to `ignore`
348+
349+
`-f` Fallback URL. Example:
350+
351+
spa-server -f "/index.html" .
352+
353+
`-F` Fallback handler function or object. Example:
354+
355+
spa-server -F "({'*': '/index.html'})" .
356+
357+
358+
`-h` or `-?` Show help
359+
360+
`-l` Log format. Defaults to `combined`. See [available formats](https://github.com/expressjs/morgan#predefined-formats)
361+
362+
`-p` Listen port. Defaults to 8888.
363+
364+
`-q` Quiet mode. Disables logging.
365+
366+
`-s` Servestatic config object. See [Servestatic options](https://github.com/expressjs/serve-static#options)
367+
368+
# Set cache-control max-age
369+
spa-server -s "({maxAge: 10000})" .
370+
# Set CORS header
371+
spa-server -s "({setHeaders: ((res, path) => {res.setHeader('Access-Control-Allow-Origin', '*')})})" .
372+
331373

332374
## Changelog
333375

bin/spa-server

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
var morgan = require('morgan'),
6+
serverFactory = require('../lib/server'),
7+
argv = require('yargs').argv,
8+
extend = require('xtend'),
9+
path = argv._[0],
10+
port = argv.p || 8888,
11+
address = argv.a || '0.0.0.0',
12+
fallbackURL = argv.f,
13+
help = argv.h || argv['?'],
14+
quiet = argv.q ,
15+
dotfiles = argv.d || 'ignore',
16+
logFormat = argv.l || 'combined',
17+
fallbackHandler = eval(argv.F),
18+
userServeStaticConfig = eval(argv.s),
19+
userMiddlewares = eval(argv.m || []),
20+
defaultServeStaticConfig = {dotfiles: dotfiles},
21+
middleware = [].concat(userMiddlewares),
22+
serveStaticConfig = extend({}, defaultServeStaticConfig, userServeStaticConfig);
23+
24+
if (help || !path) {
25+
console.log([
26+
'usage: spa-server [options] [path]',
27+
'',
28+
'options:',
29+
' -a Listen address [0.0.0.0]',
30+
' -d Show dotfiles [ignore]',
31+
' -f Fallback URL',
32+
' -F Fallback handler function or object',
33+
' -h -? Print this message and exit.',
34+
' -l Log format [combined]',
35+
' -p Listen port [8888]',
36+
' -q Disable logging',
37+
' -s Servestatic config'
38+
].join('\n'));
39+
process.exit();
40+
}
41+
42+
if (fallbackURL && fallbackHandler) {
43+
console.log('Error: Fallback URL (-f) and handler (-F) are mutually exclusive!');
44+
process.exit(1);
45+
}
46+
47+
if (!quiet) {
48+
middleware.push({
49+
middleware: morgan(logFormat),
50+
before: '$start'
51+
});
52+
}
53+
54+
var server = serverFactory.create({
55+
path: path,
56+
port: port,
57+
fallback: fallbackURL || fallbackHandler,
58+
serveStaticConfig: serveStaticConfig,
59+
hostname: address,
60+
middleware: middleware
61+
});
62+
63+
server.start();

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
"repository": "betsol/spa-server",
1717
"license": "MIT",
1818
"dependencies": {
19-
"connect": "^3.3.5",
20-
"mime": "^1.3.4",
21-
"serve-static": "^1.9.2",
22-
"xtend": "^4.0.0"
19+
"connect": "^3.6.5",
20+
"mime": "^1.4.1",
21+
"serve-static": "^1.13.1",
22+
"xtend": "^4.0.1",
23+
"yargs": "^10.0.3",
24+
"morgan": "^1.9.0"
2325
},
2426
"devDependencies": {
2527
"cheerio": "^0.19.0",
@@ -31,9 +33,13 @@
3133
"lib/",
3234
"README.md",
3335
"changelog.md",
34-
"index.js"
36+
"index.js",
37+
"bin/"
3538
],
3639
"scripts": {
3740
"test": "make test"
41+
},
42+
"bin": {
43+
"spa-server": "./bin/spa-server"
3844
}
3945
}

0 commit comments

Comments
 (0)