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 SEQUELIZE = require( 'sequelize' );
- const APP = module.exports = EXPRESS();
- APP.set( 'port', CONFIG.app.port );
- //APP.use( BODYPARSER() );
- APP.use( METHODOVERRIDE() );
- APP.use( EXPRESS.static( PATH.join( __dirname, 'public' ) ) );
- // Create the DB connection object, using the settings
- var dbo = new SEQUELIZE(
- CONFIG.db.database,
- CONFIG.db.username,
- CONFIG.db.password, {
- host: CONFIG.db.hostname,
- dialect: 'postgres',
- pool: {
- max: 5,
- min: 0,
- idle: 10000
- }
- });
- var sspColumns = {};
- // Construct the Sequelize columns object from the config.db.columns
- CONFIG.ssp.columns.forEach( (element, index, arr) => {
- sspColumns[ `dtCol_${arr[index].dt}` ] = {
- type: SEQUELIZE.STRING,
- field: arr[index].db
- };
- });
- var dtSspTable = dbo.define( CONFIG.ssp.table, sspColumns );
- APP.get( '/', ( req, res ) => {
- var pageContent = JADE.renderFile('templates/container.jade', {
- sources: CONFIG.sources,
- pageContent: JADE.renderFile('templates/datatable.jade')
- });
- res.send(pageContent);
- });
- APP.get( '/ssp', ( req, res ) => {
- var query = req.query;
- dtSspTable.findAll().then((result) => {
- var sspResult = {
- draw: query.draw,
- recordsTotal: result.length,
- recordsFiltered: result.length,
- data: []
- };
- result.forEach( (element, index, arr) => {
- var thisRow = [];
- query.columns.forEach( (cElement, cIndex, cArr) => {
- thisRow.push( arr[index].dataValues[`dtCol_${query.columns[cIndex].data}`] )
- });
- sspResult.data.push( thisRow );
- });
- res.send( JSON.stringify( sspResult ) );
- });
- });
- 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