Last active
January 4, 2017 04:08
-
-
Save ssafejava/8704372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is just an extension of https://github.com/gruntjs/grunt-contrib-connect#middleware | |
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){ | |
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; | |
}, | |
}, | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[fixes]
var serveStatic = require('serve-static');