UNPKG

@blaasvaer/frmwrk

Version:

My personal Node framework

174 lines (146 loc) 3.7 kB
/** * This is the framework frmwrk – short for a framework without all the bells and whistles. * * @author Sam Blåsvær <sam@blaasvaer.dk> * Version – See package.json file under "version" * Copyright 2018 - onwards. */ /** * Native modules */ const http = require('http'); const { Server } = require('socket.io'); let port; let _config; let _io; /** * Custom modules */ const handleRequest = require('./handle-request'); const Utils = require('./utils'); /** * Define variables */ let hostname; /** * If port number is passed in as first (custom) parameter, set port * otherwise use default 3000 * @type {Number} */ if ( ! process.env.NODE_PORT ) { port = ( process.argv.length > 2 ) ? parseInt( process.argv[ process.argv.length - 1 ] ) : 3000; } else { port = process.env.NODE_PORT; } if ( process.env.NODE_ENV !== 'production') { // console.log("ENV file", process.env.PGHOST); } /** * The framework * @param {object} config The configuration object * @return {Node} Node */ FW = function ( config ) { _config = config || {}; hostname = config.hostname ? config.hostname : '0.0.0.0'; /** * Set the Controller * @param {string} name The name of the controller * @param {object} req The incomming request object * @param {object} res The response object */ Controller = function Controller ( route, req, res, url ) { this.route = route; this.req = req; this.res = res; this.url = url; this.route.method = req.method; /** * Return view */ this.view = async function ( data = null ) { route.data = data; route.req = this.req; route.res = this.res; route.url = this.url; /** * Add search params to route */ let params = {}; for ( let pair of url.searchParams) { params[pair[0]] = pair[1]; } route.searchParams = params; let output = await route.action( route ); return output; } /** * Check for authorization parameters. */ // if ( route.params !== null ) { // console.log("Params not null", route.params); // if ( route.params.authorize !== undefined ) { // console.log("This route should be authorized!"); // Authorize( route.params ); // } // } return this; } /** * TODO: Authorize user */ Authorize = function Authorize ( credentials ) { console.log("Authorize - check credentials:", credentials); return false; // return true; } getSocketIO = () => { return _io; } /** * Install framework components and start the server */ Install( config ) .then(( result ) => { /** * Run optional post install processes here … */ /** * Create and start the server * @return {function} Server instance */ const server = http.createServer( handleRequest ).listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}`); }); if ( config.enable_socket_io ) { _io = new Server( server ); } }) .catch(function( err ) { console.log("Install Error:", err); }); return this; }; /** * Install * Setting a global install for beind able to install packages from urls etc. * Installs the components of the framework like; controllers, models, views, users etc. * @param {Object} config Config object */ global.Install = () => { global.publicPath = _config.root + '/themes/' + _config.theme; /** * Installation processes to run * @type {Array} */ let install_processes = [ require('./controllers')( _config ), require('./models')( _config ), require('./views')( _config ), require('./connections')( _config ), // require('./users')( config ) ]; return Promise.all( install_processes ); } FW.utils = Utils; module.exports = FW;