Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove server and replace with only port #104

Merged
merged 5 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:**
Expand Down Expand Up @@ -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
---
Expand Down
19 changes: 1 addition & 18 deletions expressSampleApp/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
29 changes: 0 additions & 29 deletions expressSampleApp/server2.js

This file was deleted.

110 changes: 56 additions & 54 deletions lib/reload.js
Original file line number Diff line number Diff line change
@@ -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 })
}

Expand Down Expand Up @@ -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