Skip to content

Commit

Permalink
feat(handler): Add options.handler to handle incoming request
Browse files Browse the repository at this point in the history
A function to use as main request handler (`function(req, res)`).

This provides the ability to serve the local directory. Ex:

    var finalhandler = require('finalhandler');
    var serveIndex = require('serve-index');
    var serveStatic = require('serve-static');

    tinylr({
      handler: function(req, res) {
        var done = finalhandler(req, res);
        var index = serveIndex(path.resolve('./'), {icons: true});
        var serve = serveStatic(path.resolve('./'));

        if (/changed/.test(req.url)) return this.handle(req, res);

        serve(req, res, function(err) {
          if (err) return done(err);
          index(req, res, done);
        });
      }
    });

When not defined, the default handler takes place.
  • Loading branch information
mklabs committed Apr 14, 2016
1 parent 2e3f8d0 commit 749a6ea
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ Server.prototype.configure = function configure(app) {
var self = this;
debug('Configuring %s', app ? 'connect / express application' : 'HTTP server');

var handler = this.options.handler || this.handler;

if (!app) {
if ((this.options.key && this.options.cert) || this.options.pfx) {
this.server = https.createServer(this.options, this.handler.bind(this));
this.server = https.createServer(this.options, handler.bind(this));
} else {
this.server = http.createServer(this.handler.bind(this));
this.server = http.createServer(handler.bind(this));
}
this.server.on('upgrade', this.websocketify.bind(this));
this.server.on('error', function() {
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ See [gulp-livereload](https://github.com/vohof/gulp-livereload) repo.
- `livereload` - Path to the client side lib (defaults to `path.join(__dirname, '../node_modules/livereload-js/dist/livereload.js')`)
- `port` - Livereload port (defaults to `35729`)
- `errorListener` - A callback to invoke when an error occurs (otherwise, fallbacks to standard error output)
- `handler` - A function to use as main request handler (`function(req,
res)`). When not defined, the default handler takes place.
- `app` - An express or other middleware based HTTP server
- `key` - Option to pass in to create an https server
- `cert` - Option to pass in to create an https server
Expand Down

0 comments on commit 749a6ea

Please sign in to comment.