diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-12-22 10:18:38 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-22 10:18:38 +0100 |
commit | 194ebc3da9641ff96f083f9d8ab43c2d27944f9a (patch) | |
tree | c50988f32ccf18df93ae7ab40be78e9459642818 /lib/cli/server.js | |
parent | 64ccb6b00b4b63fa0e516d4e35351275b34f8c07 (diff) | |
parent | 16af264360e48e8a833e9efa9ab8d194574dbc70 (diff) | |
download | gitbook-194ebc3da9641ff96f083f9d8ab43c2d27944f9a.zip gitbook-194ebc3da9641ff96f083f9d8ab43c2d27944f9a.tar.gz gitbook-194ebc3da9641ff96f083f9d8ab43c2d27944f9a.tar.bz2 |
Merge pull request #1543 from GitbookIO/dream
React for rendering website with plugins
Diffstat (limited to 'lib/cli/server.js')
-rw-r--r-- | lib/cli/server.js | 128 |
1 files changed, 0 insertions, 128 deletions
diff --git a/lib/cli/server.js b/lib/cli/server.js deleted file mode 100644 index 752f867..0000000 --- a/lib/cli/server.js +++ /dev/null @@ -1,128 +0,0 @@ -var events = require('events'); -var http = require('http'); -var send = require('send'); -var util = require('util'); -var url = require('url'); - -var Promise = require('../utils/promise'); - -function Server() { - this.running = null; - this.dir = null; - this.port = 0; - this.sockets = []; -} -util.inherits(Server, events.EventEmitter); - -/** - Return true if the server is running - - @return {Boolean} -*/ -Server.prototype.isRunning = function() { - return !!this.running; -}; - -/** - Stop the server - - @return {Promise} -*/ -Server.prototype.stop = function() { - var that = this; - if (!this.isRunning()) return Promise(); - - var d = Promise.defer(); - this.running.close(function(err) { - that.running = null; - that.emit('state', false); - - if (err) d.reject(err); - else d.resolve(); - }); - - for (var i = 0; i < this.sockets.length; i++) { - this.sockets[i].destroy(); - } - - return d.promise; -}; - -/** - Start the server - - @return {Promise} -*/ -Server.prototype.start = function(dir, port) { - var that = this, pre = Promise(); - port = port || 8004; - - if (that.isRunning()) pre = this.stop(); - return pre - .then(function() { - var d = Promise.defer(); - - that.running = http.createServer(function(req, res){ - // Render error - function error(err) { - res.statusCode = err.status || 500; - res.end(err.message); - } - - // Redirect to directory's index.html - function redirect() { - var resultURL = urlTransform(req.url, function(parsed) { - parsed.pathname += '/'; - return parsed; - }); - - res.statusCode = 301; - res.setHeader('Location', resultURL); - res.end('Redirecting to ' + resultURL); - } - - res.setHeader('X-Current-Location', req.url); - - // Send file - send(req, url.parse(req.url).pathname, { - root: dir - }) - .on('error', error) - .on('directory', redirect) - .pipe(res); - }); - - that.running.on('connection', function (socket) { - that.sockets.push(socket); - socket.setTimeout(4000); - socket.on('close', function () { - that.sockets.splice(that.sockets.indexOf(socket), 1); - }); - }); - - that.running.listen(port, function(err) { - if (err) return d.reject(err); - - that.port = port; - that.dir = dir; - that.emit('state', true); - d.resolve(); - }); - - return d.promise; - }); -}; - -/** - urlTransform is a helper function that allows a function to transform - a url string in it's parsed form and returns the new url as a string - - @param {String} uri - @param {Function} fn - @return {String} -*/ -function urlTransform(uri, fn) { - return url.format(fn(url.parse(uri))); -} - -module.exports = Server; |