jLinux

Untitled

Dec 8th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. const CONFIG         = require('./config');
  4. const EXPRESS        = require( 'express' );
  5. const JADE           = require( 'jade' );
  6. const FS             = require( 'fs' );
  7. const BODYPARSER     = require( 'body-parser' );
  8. const METHODOVERRIDE = require( 'method-override' );
  9. const ERRORHANDLER   = require( 'error-handler' );
  10. const HTTP           = require( 'http' );
  11. const PATH           = require( 'path' );
  12. const SEQUELIZE      = require( 'sequelize' );
  13. const APP            = module.exports = EXPRESS();
  14.  
  15.  
  16. APP.set( 'port', CONFIG.app.port );
  17. //APP.use( BODYPARSER() );
  18. APP.use( METHODOVERRIDE() );
  19. APP.use( EXPRESS.static( PATH.join( __dirname, 'public' ) ) );
  20.  
  21. // Create the DB connection object, using the settings
  22. var dbo = new SEQUELIZE(
  23.     CONFIG.db.database,
  24.     CONFIG.db.username,
  25.     CONFIG.db.password, {
  26.         host: CONFIG.db.hostname,
  27.         dialect: 'postgres',
  28.         pool: {
  29.             max: 5,
  30.             min: 0,
  31.             idle: 10000
  32.         }
  33.     });
  34.  
  35. var sspColumns = {};
  36.  
  37. // Construct the Sequelize columns object from the config.db.columns
  38. CONFIG.ssp.columns.forEach( (element, index, arr) => {
  39.     sspColumns[ `dtCol_${arr[index].dt}` ] = {
  40.         type: SEQUELIZE.STRING,
  41.         field: arr[index].db
  42.     };
  43. });
  44.  
  45.  
  46. var dtSspTable = dbo.define( CONFIG.ssp.table, sspColumns );
  47.  
  48.  
  49. APP.get( '/', ( req, res ) => {
  50.     var pageContent = JADE.renderFile('templates/container.jade', {
  51.         sources: CONFIG.sources,
  52.         pageContent: JADE.renderFile('templates/datatable.jade')
  53.     });
  54.  
  55.     res.send(pageContent);
  56. });
  57.  
  58. APP.get( '/ssp', ( req, res ) => {
  59.     var query = req.query;
  60.  
  61.     dtSspTable.findAll().then((result) => {
  62.  
  63.         var sspResult = {
  64.             draw: query.draw,
  65.             recordsTotal: result.length,
  66.             recordsFiltered: result.length,
  67.             data: []
  68.         };
  69.  
  70.         result.forEach( (element, index, arr) => {
  71.             var thisRow = [];
  72.             query.columns.forEach( (cElement, cIndex, cArr) => {
  73.                 thisRow.push( arr[index].dataValues[`dtCol_${query.columns[cIndex].data}`] )
  74.             });
  75.             sspResult.data.push( thisRow );
  76.         });
  77.  
  78.         res.send( JSON.stringify( sspResult ) );
  79.     });
  80. });
  81.  
  82. HTTP.createServer( APP ).listen( APP.get( 'port' ), () => {
  83.     console.log( `Express server listening on port ${APP.get( 'port' )}` );
  84. });
Advertisement
Add Comment
Please, Sign In to add comment