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

Add notFound function to set respondNotFound callback (#44) #54

Merged
merged 5 commits into from
Jun 23, 2016
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
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ var redbird = new require('redbird')({

##Reference

[constructor](#redbird)
[register](#register)
[unregister](#unregister)
[notFound](#notFound)
[close](#close)

<a name="redbird"/>
###Redbird(opts)

Expand Down Expand Up @@ -289,10 +295,34 @@ __Arguments__

---------------------------------------

<a name="notFound"/>
#### Redbird##notFound(callback)

Gives Redbird a callback function with two parameters, the HTTP request
and response objects, respectively, which will be called when a proxy route is
not found. The default is
```javascript
function(req, res){
res.statusCode = 404;
res.write('Not Found');
res.end();
};
```
.

__Arguments__

```javascript
src {Function(req, res)} The callback which will be called with the HTTP
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of made up this formatting for documenting a parameter that's a callback. Not sure if there's a standard for this or anything.

request and response objects when a proxy route is not found.
```

---------------------------------------

<a name="close"/>
#### Redbird##close()

Close the proxy stoping all the incoming connections.
Close the proxy, stopping all incoming connections.

---------------------------------------

Expand Down
17 changes: 12 additions & 5 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function ReverseProxy(opts){
proxy.web(req, res, {target: target});
}
}else{
notFound(res);
respondNotFound(req, res);
}
});

Expand Down Expand Up @@ -138,7 +138,7 @@ function ReverseProxy(opts){
if(target){
proxy.web(req, res, {target: target});
}else{
notFound(res);
respondNotFound(req, res);
}
});

Expand Down Expand Up @@ -174,7 +174,7 @@ function ReverseProxy(opts){
if(target){
proxy.ws(req, socket, head, {target: target});
}else{
notFound(socket);
respondNotFound(req, socket);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't an HTTP response object, but does a socket have the same set of properties and methods as an HTTP response?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it has the write and the end, I am not sure of the status method, probably not. I think that this codepath never happens in practice though, thats why it has been working so far.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it would be valid anyway to attempt to send a "not found" over a socket.
Actually, wouldn't the destination already have had to be found before the server can emit the "upgrade" event? So there would be no need for the custom error message because that should never happen in practice (unless you have the proxy settings changing in the middle of a connection or something weird like that)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats what I meant in my previous comment :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:P
If we don't seem to really care about this use case, we can perhaps open a separate issue and merge this? I haven't actually tested whether sockets work at all, so I don't know haha

}
}

Expand Down Expand Up @@ -417,11 +417,18 @@ function getSource(req){
}
*/

function notFound(res){
var respondNotFound = function(req, res){
res.statusCode = 404;
res.write('Not Found');
res.end();
}
};

ReverseProxy.prototype.notFound = function(callback){
if(typeof callback == "function")
respondNotFound = callback;
else
throw Error('notFound callback is not a function');
};

//
// Redirect to the HTTPS proxy
Expand Down