diff options
Diffstat (limited to 'src/fauxton/tasks/couchserver.js')
-rw-r--r-- | src/fauxton/tasks/couchserver.js | 73 |
1 files changed, 43 insertions, 30 deletions
diff --git a/src/fauxton/tasks/couchserver.js b/src/fauxton/tasks/couchserver.js index 0d1dc7a34..baf2b30b6 100644 --- a/src/fauxton/tasks/couchserver.js +++ b/src/fauxton/tasks/couchserver.js @@ -13,17 +13,18 @@ module.exports = function (grunt) { var log = grunt.log; - grunt.registerTask("couchserver", 'Run a couch dev proxy server', function () { + grunt.registerTask("couchserver", 'Run a couch dev proxy server', function () { var fs = require("fs"), - path = require("path"), - httpProxy = require('http-proxy'), - express = require("express"), - options = grunt.config('couchserver'), - app = express(); + path = require("path"), + http = require("http"), + httpProxy = require('http-proxy'), + send = require('send'), + options = grunt.config('couchserver'); // Options - var dist_dir = options.dist || './dist/debug/'; - var port = options.port || 8000; + var dist_dir = options.dist || './dist/debug/', + app_dir = './app', + port = options.port || 8000; // Proxy options with default localhost var proxy_settings = options.proxy || { @@ -37,44 +38,56 @@ module.exports = function (grunt) { // inform grunt that this task is async var done = this.async(); - // serve any javascript or css files from here - app.get(/\.css$|\.js$|img/, function (req, res) { - res.sendfile(path.join(dist_dir,req.url)); - }); - // create proxy to couch for all couch requests var proxy = new httpProxy.HttpProxy(proxy_settings); - // serve main index file from here - // Also proxy out to the base CouchDB host for handle_welcome_req. - // We still need to reach the top level CouchDB host even through - // the proxy. - app.get('/', function (req, res) { - var accept = req.headers.accept.split(','); - if (accept[0] == 'application/json') { - proxy.proxyRequest(req, res); - } else { - res.sendfile(path.join(dist_dir, 'index.html')); - } - }); + http.createServer(function (req, res) { + var url = req.url, + accept = req.headers.accept.split(','), + filePath; + + if (!!url.match(/assets/)) { + // serve any javascript or css files from here assets dir + filePath = path.join('./',req.url); + } else if (!!url.match(/\.css|img/)) { + filePath = path.join(dist_dir,req.url); + } else if (!!url.match(/\/js/)) { + // serve any javascript or files from dist debug dir + filePath = path.join(dist_dir,req.url); + } else if (!!url.match(/\.js$|\.html$/)) { + // server js from app directory + filePath = path.join(app_dir,req.url.replace('/_utils/fauxton/app','')); + } else if (url === '/' && accept[0] !== 'application/json') { + // serve main index file from here + filePath = path.join(dist_dir, 'index.html'); + }; + + if (filePath) { + return send(req, filePath) + .on('error', function (err) { + if (err.status === 404) { + log.writeln('Could not locate', filePath); + } else { + log.writeln('ERROR', filePath, err); + } + }) + .pipe(res); + } - app.all('*', function (req, res) { proxy.proxyRequest(req, res); - }); + }).listen(port); // Fail this task if any errors have been logged if (grunt.errors) { return false; } - var watch = grunt.util.spawn({cmd: 'bbb', grunt: true, args: ['watch']}, function (error, result, code) {/* log.writeln(String(result));*/ }); + var watch = grunt.util.spawn({cmd: 'grunt', grunt: true, args: ['watch']}, function (error, result, code) {/* log.writeln(String(result));*/ }); watch.stdout.pipe(process.stdout); watch.stderr.pipe(process.stderr); log.writeln('Listening on ' + port); - app.listen(port); - }); }; |