+ startHttpServer: function (conf) {
+ if ('httpd' in this)
+ throw new Error('httpd already running')
+ if (conf.tls == undefined)
+ this.httpd = require('http').createServer(this.httpdListener)
+ else if (!('keyFile' in conf.tls) || !('certFile' in conf.tls))
+ throw new Error('HTTPS requires a valid key and cert')
+ else
+ this.syncReads([conf.tls.keyFile, conf.tls.certFile]).then((results) => {
+ Object.defineProperty(this, 'httpsOpts', {
+ value: {
+ key: results[conf.tls.keyFile],
+ cert: results[conf.tls.certFile]
+ }
+ })
+ this.httpd =
+ require('https').createServer(this.httpsOpts, this.httpdListener)
+ })
+ this.httpd.listen(conf.port)
+ this.httpdRoot =
+ conf.httpdRoot ? require('path').normalize(conf.httpdRoot) : undefined
+ while (this.httpdRoot[this.httpdRoot.length - 1] == require('path').sep)
+ this.httpdRoot = this.httpdRoot.slice(0,-1)
+ this.syncReads(conf.skelFile, conf.clientJS, conf.hostJS)
+ .then((results) => {
+ this.skelPage = results[conf.skelFile].split('<!--STRAPP_SRC-->')
+ this.clientJS = results[conf.clientJS]
+ this.hostJS = results[conf.hostJS]
+ })
+ .catch((err) => {
+ console.log(err)
+ })
+ console.log(`HTTP${(conf.tls == undefined) ? 'S' : ''} ` +
+ `Server Started on port ${conf.port}${this.httpdRoot ?
+ `, serving files from ${this.httpdRoot}`:''}`)
+ },
+
+ /** @func
+ * @summary Create a binding for the server
+ * @desc makes a new route which is bound to a file or a path. routes
+ * bound to files always serve that file, regardless of any
+ * additional path parameters provided by the URI
+ * @arg {string} routeName - the route to create
+ * @arg {string} path - the path to the file or directory to bind
+ */
+ createBind: function (routeName, path) {
+ dlog(`Binding ${routeName} to ${path}`)
+ if (routeName in this.routes)
+ throw new Error(`route ${routeName} already exists`)
+ path = require('path').normalize(path)
+ if (this.bindJail
+ && path.indexOf(`${this.bindJail}/`) !== 0
+ && this.bindJail != path)
+ throw new Error(`${routeName}:${path} jailed to ${this.bindJail}`)
+ if (require('fs').existsSync(path)) {
+ this.route[routeName] = {
+ bind: {
+ path: path,
+ dir: require('fs').lstatSync(path).isDirectory()
+ }
+ }