github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/blockapps-ba-master/server/index.js (about)

     1  'use strict'
     2  const express = require('express');
     3  const path = require('path');
     4  const app = express();
     5  const bodyParser = require('body-parser');
     6  const ba = require('blockapps-rest');
     7  const rest = ba.rest;
     8  const common = ba.common;
     9  const config = common.config;
    10  const util = common.util;
    11  const fsutil = common.fsutil;
    12  const cors = require('cors');
    13  
    14  // read the app deployment file
    15  const deploy = fsutil.yamlSafeLoadSync(config.deployFilename, config.apiDebug);
    16  console.log('Deploy:', deploy);
    17  if (deploy === undefined) throw new Error('Deploy config.deployFilename not found ', config.deployFilename);
    18  app.set('deploy', deploy);
    19  
    20  /**
    21   * Config to handle POSTs to API
    22   *  - Parse JSON and URL encode
    23   */
    24  app.use(bodyParser.urlencoded({ extended: true }));
    25  app.use(bodyParser.json());
    26  app.use(cors());
    27  
    28  /**
    29   * Set static assets directory
    30   * for documentation
    31   */
    32  // app.use(express.static(path.join(__dirname, '../doc')));
    33  
    34  /**
    35   * Set up API routes
    36   */
    37  const routes = require('./routes');
    38  app.use('/', routes);
    39  
    40  
    41  // get the intended port number, use port 3031 if not provided
    42  const port = process.env.PORT || 3031;
    43  
    44  const server = app.listen(port, (err) => {
    45    if (err) {
    46      console.log((err.message));
    47    } else {
    48      console.log('App listening on port ' + port);
    49    }
    50  });
    51  
    52  module.exports = server;