diff --git a/README.md b/README.md index 32c97ac..e64dd40 100644 --- a/README.md +++ b/README.md @@ -69,26 +69,12 @@ app.get('/', function (req, res) { var server = http.createServer(app) // Reload code here - -// 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 - } -) +reload(app); server.listen(app.get('port'), function () { console.log('Web server listening on port ' + app.get('port')) }) + ``` **`public/index.html`:** @@ -123,14 +109,18 @@ watch.watchTree(__dirname + "/public", function (f, curr, prev) { ### API for Express ``` -reload(objectOfParameters) +reload(app, opts) ``` -`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) +#### Table of reload parameters + +| Parameter Name | Type | Description | Part of `opts` object | Optional | Default Value | +|----------------|-----------|------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------|----------|--------------------| +| app | {object} | The app. It may work with other frameworks, or even with Connect. At this time, it's only been tested with Express. | | | n/a | +| **opts** | {object} | Object of possible options (shown below) | | ✓ | n/a | +| port | {number} | Port to run reload on. | ✓ | ✓ | `9856` | +| route | {string} | Route that reload should use to serve the script file. Changing the route will require the script tag URL to change. (Recommend not modifying) | ✓ | ✓ | `reload/reload.js` | +| verbose | {boolean} | If set to true, will show logging on the server and client side. | ✓ | ✓ | `false` | Using reload as a command line application --- diff --git a/expressSampleApp/server.js b/expressSampleApp/server.js index 91222c1..56e6575 100644 --- a/expressSampleApp/server.js +++ b/expressSampleApp/server.js @@ -20,24 +20,7 @@ app.get('/', function (req, res) { var server = http.createServer(app) // Reload code here - -// 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 - } -) +reload(app) server.listen(app.get('port'), function () { console.log('Web server listening on port ' + app.get('port')) diff --git a/expressSampleApp/server2.js b/expressSampleApp/server2.js deleted file mode 100644 index 268fe11..0000000 --- a/expressSampleApp/server2.js +++ /dev/null @@ -1,29 +0,0 @@ -var express = require('express') -var http = require('http') -var path = require('path') -var reload = require('reload') -var bodyParser = require('body-parser') -var logger = require('morgan') - -var app = express() - -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 - -// add reload/reload.js route to app to server client script -reload.configureApp(app, true) - -app.get('/', function (req, res) { - res.sendFile(path.join(publicDir, 'index.html')) -}) - -var server = http.createServer(app) -// start web socket server that sends messages -reload.configureServer(server, true) - -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 3c68d80..46845b0 100644 --- a/lib/reload.js +++ b/lib/reload.js @@ -1,37 +1,77 @@ -var path = require('path') -var fs = require('fs') -var socketPortSpecified +/* + * Posible opts + * port: A port to run the reload websocket on (as a number). (Optional) (Default: `8080`) + * route: The route reload will create for the script file. (Optional) (Default `reload/reload.js`) + * verbose: Will show logging on the server and client side. (Optional) (Default: `false`) + */ +function reload (app, opts) { + // Requires + var path = require('path') + var fs = require('fs') -var RELOAD_FILE = path.join(__dirname, './reload-client.js') + // Parameters variables + var httpServerOrPort + var expressApp + var verboseLogging + var port -function configureApp (expressApp, verboseLogging, route) { + // Application variables + var RELOAD_FILE = path.join(__dirname, './reload-client.js') var reloadCode = fs.readFileSync(RELOAD_FILE, 'utf8') + var route + // Websocket server variables + var connections = new Set() + var WebSocketServer = require('ws').Server + var wss + + // General variables + var socketPortSpecified + + opts = opts || {} + + if (arguments.length > 0 && (typeof (arguments[0]) === 'number' || typeof (arguments[0]) === 'object')) { + if (typeof (arguments[0]) === 'number') { // If old arguments passed handle old arguments, the old arguments and their order were: httpServerOrPort, expressApp, verboseLogging, + console.warn('Deprecated Warning: You supplied reload old arguments, please upgrade to the new parameters 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 { // Setup options or use defaults + expressApp = arguments[0] + port = opts.port || 9856 + route = opts.route || '/reload/reload.js' + verboseLogging = opts.verbose === true || opts.verbose === 'true' || false + + if (port) { + socketPortSpecified = port + httpServerOrPort = port + } + } + } else { + throw new Error('Lack of/invalid arguments provided to reload') + } + + // Application setup if (verboseLogging) { reloadCode = reloadCode.replace('verboseLogging = false', 'verboseLogging = true') } if (socketPortSpecified) { - reloadCode = reloadCode.replace('window.location.origin.replace()', 'window.location.origin.replace(/(^http(s?):\\/\\/)(.*:)(.*)/, \'ws$2://$3' + socketPortSpecified + '\')') - } else { - reloadCode = reloadCode.replace('window.location.origin.replace()', 'window.location.origin.replace(/^http(s?):\\/\\//, \'ws$1://\')') + reloadCode = reloadCode.replace('window.location.origin.replace()', 'window.location.origin.replace(/(^http(s?):\\/\\/)(.*:)(.*)/,' + (socketPortSpecified ? '\'ws$2://$3' + socketPortSpecified : 'ws$2://$3$4') + '\')') } - expressApp.get(route || '/reload/reload.js', function (req, res) { + expressApp.get(route, function (req, res) { res.type('text/javascript') res.send(reloadCode) }) -} - -function configureServer (httpServerOrPort, verboseLogging) { - var connections = new Set() - var WebSocketServer = require('ws').Server - var wss + // Websocket server setup // Use custom user specified port if (socketPortSpecified) { wss = new WebSocketServer({ port: httpServerOrPort }) - } else { // Attach to server, using server's port + } else { // Attach to server, using server's port. Kept here to support legacy arguments. wss = new WebSocketServer({ server: httpServerOrPort }) } @@ -69,42 +109,4 @@ function configureServer (httpServerOrPort, verboseLogging) { } } -/* - * 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) { - 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] - expressApp = arguments[1] - verboseLogging = arguments[2] - - socketPortSpecified = typeof httpServerOrPort === 'number' ? httpServerOrPort : null - } else { - httpServerOrPort = objectOfParameters.server - port = objectOfParameters.port - expressApp = objectOfParameters.app - verboseLogging = objectOfParameters.verbose === true || objectOfParameters.verbose === 'true' || false - - if (port) { - socketPortSpecified = port - httpServerOrPort = port - } - } - - configureApp(expressApp, verboseLogging) - return configureServer(httpServerOrPort, verboseLogging) -} - module.exports = reload -module.exports.configureApp = configureApp -module.exports.configureServer = configureServer