Code coverage report for lib/server.js

Statements: 100% (64 / 64)      Branches: 100% (28 / 28)      Functions: 100% (10 / 10)      Lines: 100% (64 / 64)     

All files » lib/ » server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 1181                   1 4 4 2 2 1   1   4     1   1 5     8     8 8 3 1   2 2     5 5 1 1   4 4             5 1 1   5 1   5   5 2     5 5 5 5 5     5   5   5   5 1 1 1 1         5 5 5 5   5     5 2 1   2 2 2   2     5 3 2   3 2     5    
var util = require('./index'),
    path = require('path'),
    url = require('url'),
    fs = require('fs'),
    express = require('express'),
    echoecho = require('echoecho'),
    combo = require('combohandler'),
    log = require('./log'),
    server;
 
var showError = function(options, e) {
    var str = 'Grover Error\n';
    if (e.code === 'EADDRINUSE') {
        str += 'Port ' + options.port + ' is in use, try a different one!\n';
    } else if (e.code === 'EACCES') {
        str += 'You do not have access to port ' + options.port + '!\n';
    } else {
        str += e;
    }
    return str;
};
 
exports.showError = showError;
 
exports.start = function(options, callback) {
    var pre = '/',
        root = options.server,
        handler = function (req, res) {
            var u = url.parse(req.url),
                p = path.join(root, u.pathname);
 
            util.exists(p, function(x) {
                if (!x) {
                    if (echoecho.handle(req)) {
                        echoecho.serve(req, res);
                    } else {
                        res.statusCode = 404;
                        res.end('Not Found', 'utf8');
                    }
                } else {
                    fs.readFile(p, 'utf8', function(err, data) {
                        if (err) {
                            res.statusCode = 404;
                            res.end('Not Found', 'utf8');
                        } else {
                            res.statusCode = 200;
                            res.end(data, 'utf8');
                        }
                    });
                }
            });
        };
 
    if (!options.silent && !options.quiet) {
        util.log('  starting grover server');
        util.log('  assuming server root as ' + process.cwd());
    }
    if (options.prefix) {
        pre = options.prefix;
    }
    options.prefix = 'http:/'+'/127.0.0.1:' + options.port;
 
    if (!options.run) {
        util.log('listening on: ' + options.prefix);
    }
 
    options.paths.forEach(function(url, key) {
        url = url.replace(root, ''); //If they are full filesytem paths, we replace the root
        var URI = options.prefix + path.join(pre, url);
        URI = URI.split(path.sep).join('/');
        options.paths[key] = URI;
    });
 
    echoecho.paths(options.paths);
 
    server = express.createServer();
 
    server.use(express.bodyParser());
 
    if (options.combo && options.combo.length) {
        options.combo.forEach(function(info) {
            util.log('  attaching combo to: ' + info.route);
            server.get(info.route, combo.combine({ rootPath: info.root }), function (req, res) {
                res.send(res.body, 200);
            });
        });
    }
 
    server.get('/'+'*', handler);
    server.post('/'+'*', handler);
    server['delete']('/'+'*', handler);
    server.put('/'+'*', handler);
 
    options.httpd = server;
 
 
    server.on('error', function(e) {
        if (callback) {
            callback(e, server);
        }
        log.error(util.color(showError(options, e), 'red'));
        try {
        server.close();
        } catch (e) {}
        util.exit(1);
    });
 
    server.on('listening', function() {
        if (callback) {
            callback(null, server);
        }
        if (process.send) { //We are a child process
            process.send({ serving: true, server: server });
        }
    });
    server.listen(options.port);
};