From 0e3660ca732f3ef276ac05cfdacf2796699fd841 Mon Sep 17 00:00:00 2001 From: "Alexander J. Lallier" Date: Mon, 19 Jun 2017 09:00:03 -0400 Subject: [PATCH 1/2] Removed all old API arguments and replaced with an object of parameters. * Updated README with API changes * Updated sample app --- README.md | 43 +++++++++++++++++++++++++------------- expressSampleApp/server.js | 21 +++++++++++++++++-- lib/reload.js | 32 ++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 4e27d52..32c97ac 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Reload can be used in conjunction with tools that allow for automatically restar var express = require('express') var http = require('http') var path = require('path') -var reload = require('reload') +var reload = require('../../reload') var bodyParser = require('body-parser') var logger = require('morgan') @@ -60,24 +60,35 @@ var publicDir = path.join(__dirname, 'public') app.set('port', process.env.PORT || 3000) app.use(logger('dev')) -app.use(bodyParser.json()) //parses json, multi-part (file), url-encoded +app.use(bodyParser.json()) // Parses json, multi-part (file), url-encoded -app.get('/', function(req, res) { +app.get('/', function (req, res) { res.sendFile(path.join(publicDir, 'index.html')) }) var server = http.createServer(app) // Reload code here -// Reload attaching to server's port -reload(server, app) - -// Reload using a custom port to run the websocket on -reload(8080, app); -server.listen(app.get('port'), function(){ - console.log("Web server listening on port " + app.get('port')); -}); +// Reload attaching to server's port +reload( + { + server: server, + app: app + } +) + +// Or Reload using a custom port to run the websocket on +reload( + { + port: 8080, + app: app + } +) + +server.listen(app.get('port'), function () { + console.log('Web server listening on port ' + app.get('port')) +}) ``` **`public/index.html`:** @@ -112,12 +123,14 @@ watch.watchTree(__dirname + "/public", function (f, curr, prev) { ### API for Express ``` -reload(httpServerOrPort, expressApp, [verbose]) +reload(objectOfParameters) ``` -- `httpServerOrPort`: The Node.js http server from the module `http` **or** a port to run the reload websocket on (as a number). **Note**: It is important to specify a custom port if you have other websockets running in your application so they don't conflict. -- `expressApp`: The express app. It may work with other frameworks, or even with Connect. At this time, it's only been tested with Express. -- `verbose`: If set to true, will show logging on the server and client side +`objectOfParameters` is an object containing the possible following parameters: +- `server`: The Node.js http server from the module `http` (Optional, but if omitted port is required.) +- `port`: A port to run the reload websocket on (as a number). **Note**: It is important to specify a custom port if you have other websockets running in your application so they don't conflict. (Optional, but if omitted server is required.) +- `app`: The express app. It may work with other frameworks, or even with Connect. At this time, it's only been tested with Express. +- `verbose`: If set to true, will show logging on the server and client side. (Optional) Using reload as a command line application --- diff --git a/expressSampleApp/server.js b/expressSampleApp/server.js index e3706d5..91222c1 100644 --- a/expressSampleApp/server.js +++ b/expressSampleApp/server.js @@ -1,7 +1,7 @@ var express = require('express') var http = require('http') var path = require('path') -var reload = require('reload') +var reload = require('../../reload') var bodyParser = require('body-parser') var logger = require('morgan') @@ -20,7 +20,24 @@ app.get('/', function (req, res) { var server = http.createServer(app) // Reload code here -reload(server, app, true) + +// Reload attaching to server's port +reload( + { + server: server, + app: app, + verbose: false + } +) + +// Or Reload using a custom port to run the websocket on +reload( + { + port: 8080, + app: app, + verbose: false + } +) server.listen(app.get('port'), function () { console.log('Web server listening on port ' + app.get('port')) diff --git a/lib/reload.js b/lib/reload.js index 4512ec4..84e4fd1 100644 --- a/lib/reload.js +++ b/lib/reload.js @@ -69,8 +69,36 @@ function configureServer (httpServerOrPort, verboseLogging) { } } -function reload (httpServerOrPort, expressApp, verboseLogging) { - socketPortSpecified = typeof httpServerOrPort === 'number' ? httpServerOrPort : null +/* + * Posible parametes in objectOfParameters + * server: The Node.js http server from the module `http` (Optional, but if omitted port is required.) + * port: A port to run the reload websocket on (as a number). (Optional, but if omitted server is required.) + * app: The express app. + * verbose: Will show logging on the server and client side. (Optional) + */ +function reload (objectOfParameters) { + if (arguments.length > 1) { // If old arguments passed, these were the old arguments and their order: httpServerOrPort, expressApp, verboseLogging + console.warn('Deprecated Warning: You supplied reload old arguments, please upgrade to the new object parameter see: https://github.com/jprichardson/reload/tree/master#api-for-express') + httpServerOrPort = arguments[0] + expressApp = arguments[1] + verboseLogging = arguments[2] + + socketPortSpecified = typeof httpServerOrPort === 'number' ? httpServerOrPort : null + } + else { + httpServer = objectOfParameters.server; + port = objectOfParameters.port; + expressApp = objectOfParameters.app; + verboseLogging = objectOfParameters.verbose === true || objectOfParameters.verbose === 'true' ? true : false; + + httpServerOrPort = httpServer + + if (port) { + socketPortSpecified = port; + httpServerOrPort = port + } + } + configureApp(expressApp, verboseLogging) return configureServer(httpServerOrPort, verboseLogging) } From b58c865e2aa91e2076f0804b11b235c903939bf5 Mon Sep 17 00:00:00 2001 From: "Alexander J. Lallier" Date: Mon, 19 Jun 2017 09:21:45 -0400 Subject: [PATCH 2/2] Fix standard errors --- lib/reload.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/reload.js b/lib/reload.js index 84e4fd1..3c68d80 100644 --- a/lib/reload.js +++ b/lib/reload.js @@ -77,6 +77,11 @@ function configureServer (httpServerOrPort, verboseLogging) { * verbose: Will show logging on the server and client side. (Optional) */ function reload (objectOfParameters) { + var httpServerOrPort + var expressApp + var verboseLogging + var port + if (arguments.length > 1) { // If old arguments passed, these were the old arguments and their order: httpServerOrPort, expressApp, verboseLogging console.warn('Deprecated Warning: You supplied reload old arguments, please upgrade to the new object parameter see: https://github.com/jprichardson/reload/tree/master#api-for-express') httpServerOrPort = arguments[0] @@ -84,17 +89,14 @@ function reload (objectOfParameters) { verboseLogging = arguments[2] socketPortSpecified = typeof httpServerOrPort === 'number' ? httpServerOrPort : null - } - else { - httpServer = objectOfParameters.server; - port = objectOfParameters.port; - expressApp = objectOfParameters.app; - verboseLogging = objectOfParameters.verbose === true || objectOfParameters.verbose === 'true' ? true : false; - - httpServerOrPort = httpServer + } else { + httpServerOrPort = objectOfParameters.server + port = objectOfParameters.port + expressApp = objectOfParameters.app + verboseLogging = objectOfParameters.verbose === true || objectOfParameters.verbose === 'true' || false if (port) { - socketPortSpecified = port; + socketPortSpecified = port httpServerOrPort = port } }