Skip to content

Instantly share code, notes, and snippets.

@ssafejava
Last active January 4, 2017 04:08
Show Gist options
  • Save ssafejava/8704372 to your computer and use it in GitHub Desktop.
Save ssafejava/8704372 to your computer and use it in GitHub Desktop.

Revisions

  1. ssafejava revised this gist Jan 30, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // This is just an extension of https://github.com/gruntjs/grunt-contrib-connect#middleware
    grunt.initConfig({
    connect: {
    server: {
  2. ssafejava revised this gist Jan 30, 2014. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,15 @@ grunt.initConfig({
    // Not found - just serve index.html
    // ***
    middlewares.push(function(req, res){
    require('fs').createReadStream(options.base + "/index.html").pipe(res);
    for(var file, i = 0; i < options.base.length; i++){
    file = options.base + "/index.html";
    if (grunt.file.exists(file)){
    require('fs').createReadStream(file).pipe(res);
    return; // we're done
    }
    }
    res.statusCode(404); // where's index.html?
    res.end();
    });
    return middlewares;
    },
  3. ssafejava created this gist Jan 30, 2014.
    29 changes: 29 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    grunt.initConfig({
    connect: {
    server: {
    options: {
    middleware: function(connect, options) {
    var middlewares = [];
    if (!Array.isArray(options.base)) {
    options.base = [options.base];
    }
    var directory = options.directory || options.base[options.base.length - 1];
    options.base.forEach(function(base) {
    // Serve static files.
    middlewares.push(connect.static(base));
    });
    // Make directory browse-able.
    middlewares.push(connect.directory(directory));

    // ***
    // Not found - just serve index.html
    // ***
    middlewares.push(function(req, res){
    require('fs').createReadStream(options.base + "/index.html").pipe(res);
    });
    return middlewares;
    },
    },
    },
    },
    });