Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- const CONFIG = require('./config');
- const EXPRESS = require( 'express' );
- const JADE = require( 'jade' );
- const FS = require( 'fs' );
- const BODYPARSER = require( 'body-parser' );
- const METHODOVERRIDE = require( 'method-override' );
- const ERRORHANDLER = require( 'error-handler' );
- const HTTP = require( 'http' );
- const PATH = require( 'path' );
- const APP = module.exports = EXPRESS();
- const SEQUELIZE = require( 'sequelize' );
- APP.set( 'models', require('./models') );
- APP.set( 'port', CONFIG.app.port );
- APP.use( METHODOVERRIDE() );
- APP.use( EXPRESS.static( PATH.join( __dirname, 'public' ) ) );
- //APP.use( BODYPARSER() );
- // Load the Server Side Processing model
- var SSP = APP.get('models').SSP;
- // Page loading the HTML DataTable
- APP.get( '/', ( req, res ) => {
- var pageContent = JADE.renderFile('templates/container.jade', {
- sources: CONFIG.sources,
- pageContent: JADE.renderFile('templates/datatable.jade')
- });
- res.send( pageContent );
- });
- // Start a new SSP result by creating an object with the draw integer
- function newSspResult( sspParams ) {
- return new Promise(( resolve, reject ) => {
- resolve(sspParams, {
- draw: parseInt( sspParams.draw ),
- data: []
- });
- })
- }
- // Get the total rows in the table, append it to the SSP result object
- function totalRows( sspParams, sspResult ) {
- console.log('sspResult',typeof sspResult);
- return new Promise( ( resolve, reject ) => {
- SSP.findOne({
- attributes: [[SEQUELIZE.fn('COUNT', SEQUELIZE.col('*')), 'total_rows']]
- }).then(( result ) => {
- sspResult.recordsTotal = result.dataValues.total_rows;
- resolve( sspParams, sspResult );
- });
- })
- }
- function execSspQuery( sspParams, sspResult ){
- var query = {};
- if(typeof sspParams.length !== 'undefined' && parseInt(sspParams.length) !== 0)
- query.limit = parseInt(sspParams.length);
- if(typeof sspParams.start !== 'undefined')
- query.offset = parseInt(sspParams.start);
- query.order = [];
- // Check if were supposed to be ordering anything..
- if( typeof sspParams.order !== 'undefined' )
- sspParams.order.forEach(( elem, key, arr ) => {
- query.order.push([ SEQUELIZE.col(`dtCol_${elem.column}` ), elem.dir]);
- });
- SSP.findAll( query ).then( ( result ) => {
- result.forEach( (element, index, arr) => {
- var thisRow = [];
- sspParams.columns.forEach( (cElement, cIndex, cArr) => {
- thisRow.push( element.dataValues[`dtCol_${cElement.data}`] )
- });
- sspResult.data.push( thisRow );
- });
- resolve( sspResult );
- });
- }
- SSP.utils.APP = APP;
- APP.get( '/ssp', ( req, res ) => {
- // Inbound SSP Query
- var sspParams = req.query;
- var dbQuery = {};
- newSspResult( sspParams )
- .then(totalRows)
- .then(execSspQuery)
- .then(( sspResult ) => {
- console.log( 'sspResult',sspResult );
- res.send( JSON.stringify( sspResult ) );
- })
- .catch((err) => {console.log(`ERROR: ${err}`)});
- });
- HTTP.createServer( APP ).listen( APP.get( 'port' ), ( ) => {
- console.log( `Express server listening on port ${APP.get( 'port' )}` );
- });
Advertisement
Add Comment
Please, Sign In to add comment