httpster
Version:
Simple http server for static content
133 lines (100 loc) • 3.81 kB
JavaScript
// Generated by CoffeeScript 1.12.7
/*
HTTPster
Copyright(c) 2018 Simeon Bateman <simeon@simb.net>
MIT Licensed
*/
(function() {
var basicAuth, compression, cors, env, exec, express, fav, fs, morgan, pack, path, port, program, ref, ref1, ref2, ref3, ref4, ref5, router, serveIndex, startDefaultServer, useCompress, useCors, usePushstate, useSymlink;
fs = require('fs');
program = require('commander');
exec = require('child_process').exec;
express = require('express');
serveIndex = require('serve-index');
cors = require('cors');
compression = require('compression');
fs = require('fs');
path = "./";
port = void 0;
fav = require('./fav');
pack = require('../package.json');
env = require('node-env-file');
basicAuth = require('basic-auth');
morgan = require('morgan');
program.version(pack.version).option('-p, --port <port>', 'Port for server to run on - defaults to 3333').option('-d, --dir [path]', 'Server directory - defaults to ./').option('-z, --compress', 'Add support for compression').option('-s, --pushstate', 'Add support for HTML5 pushstate').option('-e, --env', 'Add support for setting environmental variables from .env file').option('-b, --basic_auth', 'Add support for basic auth security. Uses environmental variables HTTPSTER_AUTH_USER and HTTPSTER_AUTH_PASS to authenticate').option('-c, --cors', 'Add cors support').option('-l, --symlink', 'Enable symlink').parse(process.argv);
if (program.env) {
env(fs.realpathSync(path) + "/.env");
}
port = (ref = program.port) != null ? ref : 3333;
path = (ref1 = program.dir) != null ? ref1 : fs.realpathSync(path);
useCompress = (ref2 = program.compress) != null ? ref2 : false;
useCors = (ref3 = program.cors) != null ? ref3 : false;
usePushstate = (ref4 = program.pushstate) != null ? ref4 : false;
useSymlink = (ref5 = program.symlink) != null ? ref5 : false;
router = express.Router();
if (program.basic_auth) {
if (process.env.HTTPSTER_AUTH_USER == null) {
throw 'HTTPSTER Basic Authentication Enabled but no HTTPSTER_AUTH_USER environmental variable was found';
}
if (process.env.HTTPSTER_AUTH_PASS == null) {
throw 'HTTPSTER Basic Authentication Enabled but no HTTPSTER_AUTH_PASS environmental variable was found';
}
}
router.use(function(req, res, next) {
var err, isSymlink;
isSymlink = void 0;
try {
fs.lstat(req.url.replace('/', './'), function(err, stat) {
try {
isSymlink = stat.isSymbolicLink();
} catch (error) {
err = error;
}
try {
if (isSymlink === true && useSymlink === false) {
throw new Error('Symlinks not allowed');
}
} catch (error) {
err = error;
next(err);
}
});
} catch (error) {
err = error;
next(err);
}
next();
});
startDefaultServer = function(port, path) {
var app;
app = express();
if (useCompress === true) {
app.use(compression());
}
app.use(fav(path));
if (program.basic_auth) {
app.use(basicAuth(process.env.HTTPSTER_AUTH_USER, process.env.HTTPSTER_AUTH_PASS));
}
if (useCors) {
app.use(cors());
}
app.use(router);
app.use(express["static"](path));
app.use(serveIndex(path));
app.use(morgan("dev"));
if (usePushstate === true) {
app.all("/*", function(req, res) {
res.sendfile("index.html", {
root: path
});
});
}
if (app.get('env') === 'production') {
app.use(express.staticCache());
}
app.listen(parseInt(port, 10));
return app;
};
console.log("Starting HTTPster v%s on port %j from %s", pack.version, port, path);
startDefaultServer(port, path);
}).call(this);