UNPKG

frontail

Version:

streaming logs to the browser

76 lines (64 loc) 1.62 kB
'use strict'; const connect = require('connect'); const fs = require('fs'); const serveStatic = require('serve-static'); const expressSession = require('express-session'); const basicAuth = require('basic-auth-connect'); function ConnectBuilder(urlPath) { this.app = connect(); this.urlPath = urlPath; } ConnectBuilder.prototype.authorize = function authorize(user, pass) { this.app.use( this.urlPath, basicAuth( (incomingUser, incomingPass) => user === incomingUser && pass === incomingPass ) ); return this; }; ConnectBuilder.prototype.build = function build() { return this.app; }; ConnectBuilder.prototype.index = function index( path, files, filesNamespace, themeOpt ) { const theme = themeOpt || 'default'; this.app.use(this.urlPath, (req, res) => { fs.readFile(path, (err, data) => { res.writeHead(200, { 'Content-Type': 'text/html', }); res.end( data .toString('utf-8') .replace(/__TITLE__/g, files) .replace(/__THEME__/g, theme) .replace(/__NAMESPACE__/g, filesNamespace) .replace(/__PATH__/g, this.urlPath), 'utf-8' ); }); }); return this; }; ConnectBuilder.prototype.session = function sessionf(secret) { this.app.use( this.urlPath, expressSession({ secret, resave: false, saveUninitialized: true, }) ); return this; }; ConnectBuilder.prototype.static = function staticf(path) { this.app.use(this.urlPath, serveStatic(path)); return this; }; module.exports = (urlPath) => new ConnectBuilder(urlPath);