embark-area-51/lib/modules/webserver/server.js

47 lines
1.5 KiB
JavaScript

let finalhandler = require('finalhandler');
let http = require('http');
let serveStatic = require('serve-static');
const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host');
require('http-shutdown').extend();
class Server {
constructor(options) {
this.dist = options.dist || 'dist/';
this.port = options.port || 8000;
this.hostname = dockerHostSwap(options.host) || defaultHost;
}
start(callback) {
if (this.server && this.server.listening) {
let message = __("a webserver is already running at") + " " +
("http://" + canonicalHost(this.hostname) +
":" + this.port).bold.underline.green;
return callback(null, message);
}
let serve = serveStatic(this.dist, {'index': ['index.html', 'index.htm']});
this.server = http.createServer(function onRequest(req, res) {
serve(req, res, finalhandler(req, res));
}).withShutdown();
let message = __("webserver available at") +
" " +
("http://" + canonicalHost(this.hostname) +
":" + this.port).bold.underline.green;
this.server.listen(this.port, this.hostname);
callback(null, message);
}
stop(callback) {
if (!this.server || !this.server.listening) {
return callback(null, __("no webserver is currently running"));
}
this.server.shutdown(function() {
callback(null, __("Webserver stopped"));
});
}
}
module.exports = Server;