|
| 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(); |
0 commit comments