From 54333561c7eb3690fb050645805a67ec5c43ea16 Mon Sep 17 00:00:00 2001 From: Unitech Date: Sun, 29 Jan 2017 17:30:20 +0100 Subject: [PATCH] (serve) various fixes --- bin/pm2 | 5 ++--- lib/API.js | 37 ------------------------------------- lib/API/Extra.js | 34 ++++++++++++++++++++++++++++++++++ lib/API/Serve.js | 5 ++--- 4 files changed, 38 insertions(+), 43 deletions(-) diff --git a/bin/pm2 b/bin/pm2 index 85636165..f90c4fcf 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -808,11 +808,10 @@ commander.command('deepUpdate') // Launch a http server that expose a given path on given port // commander.command('serve [path] [port]') + .alias('expose') .description('serve a directory over http via port') .action(function (path, port) { - commander.path = path || commander.path; - commander.port = port || commander.port; - pm2.serve(commander); + pm2.serve(path, port); }); // diff --git a/lib/API.js b/lib/API.js index 2bcd6eb8..6efa9048 100644 --- a/lib/API.js +++ b/lib/API.js @@ -1630,40 +1630,3 @@ API.prototype.deepUpdate = function(cb) { cb ? cb(null, {success:true}) : that.exitCli(conf.SUCCESS_EXIT); }); }; - -/** - * API method to launch a process that will serve directory over http - * - * @param {Object} opts options - * @param {String} opts.path path to be served - * @param {Number} opts.port port on which http will bind - * @param {Function} cb optional callback - */ -API.prototype.serve = function (opts, cb) { - var that = this; - - if (typeof (opts) === 'function') { - cb = opts; - servePath = opts; - } - - var servePort = process.env.PM2_SERVE_PORT || opts.port || 8080; - var servePath = path.resolve(process.env.PM2_SERVE_PATH || opts.path || '.'); - - // allow to override configuration via commander - if (!opts.env) - opts.env = {} - opts.env['PM2_SERVE_PORT'] = opts.env['PM2_SERVE_PORT'] || servePort; - opts.env['PM2_SERVE_PATH'] = opts.env['PM2_SERVE_PATH'] || servePath; - opts.name = opts.name || servePath; - - - this.start(path.resolve(__dirname, 'API', 'Serve.js'), opts, function (err, res) { - if (err) { - Common.printError(conf.PREFIX_MSG_ERR + 'Error while trying to serve : ' + err.message || err); - return cb ? cb(err) : that.exitCli(conf.ERROR_EXIT); - } - Common.printOut(conf.PREFIX_MSG + 'Serving ' + servePath + ' on port ' + servePort); - return cb ? cb(null, res) : that.exitCli(conf.SUCCESS_EXIT); - }); -} diff --git a/lib/API/Extra.js b/lib/API/Extra.js index b7983ff0..9257de6b 100644 --- a/lib/API/Extra.js +++ b/lib/API/Extra.js @@ -255,6 +255,40 @@ module.exports = function(CLI) { }; + /** + * API method to launch a process that will serve directory over http + * + * @param {Object} opts options + * @param {String} opts.path path to be served + * @param {Number} opts.port port on which http will bind + * @param {Function} cb optional callback + */ + CLI.prototype.serve = function (target_path, port, cb) { + var that = this; + + var servePort = process.env.PM2_SERVE_PORT || port || 8080; + var servePath = path.resolve(process.env.PM2_SERVE_PATH || target_path || '.'); + + var filepath = path.resolve(path.dirname(module.filename), './Serve.js'); + + this.start({ + script : filepath, + name : 'static-page-server(' + servePort + ')', + cwd : servePath, + env : { + PM2_SERVE_PORT : servePort, + PM2_SERVE_PATH : servePath + } + }, function (err, res) { + if (err) { + Common.printError(cst.PREFIX_MSG_ERR + 'Error while trying to serve : ' + err.message || err); + return cb ? cb(err) : that.exitCli(cst.ERROR_EXIT); + } + Common.printOut(cst.PREFIX_MSG + 'Serving ' + servePath + ' on port ' + servePort); + return cb ? cb(null, res) : that.exitCli(cst.SUCCESS_EXIT); + }); + } + /** * Ping daemon - if PM2 daemon not launched, it will launch it * @method ping diff --git a/lib/API/Serve.js b/lib/API/Serve.js index ea7b79a1..d5a7f6bf 100644 --- a/lib/API/Serve.js +++ b/lib/API/Serve.js @@ -210,8 +210,8 @@ http.createServer(function (request, response) { fs.readFile(filePath, function (error, content) { if (error) { - debug('[%s] Error while serving %s with content-type %s : %s', - Date.now(), filePath, contentType, error.message || err); + console.error('[%s] Error while serving %s with content-type %s : %s', + Date.now(), filePath, contentType, error.message || error); if (error.code === 'ENOENT') { fs.readFile(options.path + '/404.html', function (err, content) { content = err ? '404 Not Found' : content; @@ -235,4 +235,3 @@ http.createServer(function (request, response) { } console.log('Exposing %s directory on port %d', options.path, options.port); }); -