UNPKG

patriot

Version:

Patriot command-line interface and node.js library.

86 lines (68 loc) 1.86 kB
/*! * Module dependencies. */ var Command = require('./util/command'), project = require('./util/project'), util = require('util'), express = require('express'), serve = express(); /*! * Command setup. */ module.exports = { create: function (patriot) { return new ServeCommand(patriot); } }; function ServeCommand(patriot) { return Command.apply(this, arguments); } util.inherits(ServeCommand, Command); /** * Serve the App. * * Creates a local server to serve up the project. The intended * receiver is the Patriot App but any browser can consume the * content. * * Options: * * - `options` {Object} * - `[port]` {Number} is the server port (default: 3000). * - `[callback]` {Function} is triggered when server starts. * - `e` {Error} is null unless there is an error. * * Returns: * * {Patriot} for chaining. */ ServeCommand.prototype.run = function (options, callback) { // require options if (!options) throw new Error('requires option parameter'); // optional parameters options.port = options.port || 3000; options.folder = options.folder || "www-dev"; callback = callback || function () { }; // start app this.execute(options, callback); return this.patriot; }; /*! * Execute command. */ ServeCommand.prototype.execute = function (options, callback) { var self = this; // change to project directory and delegate errors if (!project.cd({ emitter: self.patriot, callback: callback })) return; serve.configure(function () { serve.use(express.static(options.folder + "/")); }); // start the server self.patriot.emit('log', 'listening on 127.0.0.1:' + options.port); callback(null, { address: '127.0.0.1', port: options.port }); serve.listen(options.port); };