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

Removed all old API arguments and replaced with an object of parameters. (Closes #77) #101

Merged
merged 2 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
43 changes: 28 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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`:**
Expand Down Expand Up @@ -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
---
Expand Down
21 changes: 19 additions & 2 deletions expressSampleApp/server.js
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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'))
Expand Down
34 changes: 32 additions & 2 deletions lib/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,38 @@ 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) {
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)
}
Expand Down