startserve
Version:
Startserve is an open source command line http server for serving static files.
87 lines (72 loc) • 2.81 kB
JavaScript
(function() {
"use strict";
var http = require('http'),
finalhandler = require('finalhandler'),
serveStatic = require('serve-static'),
product = require('../package.json');
/**
* Display short information on Startserve.
*/
var about = function() {
console.info('\n\n\tAbout %s | Version: %s', product.name, product.version);
console.info('\t-----------------------------------------------------------------------');
console.info('\t Project Name : %s', product.name);
console.info('\t Developed & maintained by author: %s', product.author);
console.info('\t Follow me at: (Github | Linkedin): hegdeashwin');
console.info('\n\tGithub Information:');
console.info('\t-----------------------------------------------------------------------');
console.info('\t Repository: %s', product.repository.url);
console.info('\t Issues: %s', product.issues.url);
console.info('\t Milestones: %s', product.milestones.url);
console.info('\t Releases: %s', product.releases.url);
console.info('\t Wiki: %s', product.wiki.url);
console.info('\n\t-----------------------------------------------------------------------');
console.info('\tThe MIT License (MIT), Copyright © 2014-2015; %s.', product.author);
console.info('\tFor more information visit: %s.\n\n', product.licenses.url);
process.exit(1);
};
var startserve = function(options, callback) {
/**
* Set Startserve's default options.
*/
if (typeof options === 'function') {
callback = options;
options = null;
}
if (options === null) {
options = {};
}
if (options.port === null) {
options.port = 9000;
}
if (options.gateway === null) {
options.gateway = '.';
}
if (options.about) {
about();
}
if (callback === null) {
callback = Function.prototype;
}
/**
* Setting gateway pages.
*/
var serve = serveStatic(options.gateway, {
'index': ['index.html', 'index.htm']
});
/**
* Create a HTTP server.
*/
var server = http.createServer(function(req, res) {
var done = finalhandler(req, res);
serve(req, res, done);
});
/**
* Listen to port.
*/
server.listen(options.port);
console.log("\n\n\t%s server is running at http://localhost:%s or http://127.0.0.1:%s", product.name, options.port, options.port);
console.log("\n\tPress `ctrl + c` to stop.");
};
module.exports = startserve;
}());