mirror of
https://github.com/Unitech/pm2.git
synced 2025-12-08 20:35:53 +00:00
(serve) various fixes
This commit is contained in:
parent
4b82c694b6
commit
54333561c7
5
bin/pm2
5
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);
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
37
lib/API.js
37
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);
|
||||
});
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user