require('app-module-path').addPath(__dirname); const express = require('express'); const app = express(); const conf = require('utils/config'); console.log(conf.get('redis.host')); // setup generate request-id middleware const addRequestId = require('express-request-id')(); app.use(addRequestId); // setup express-session with redis store const session = require('express-session'); const RedisStore = require('connect-redis')(session); const uuidV4 = require('uuid/v4'); app.use(session({ genid: function(req) { return uuidV4(); // generates session id using UUID }, store: new RedisStore({ host: conf.get('redis.host'), port: conf.get('redis.port'), ttl: 180 }), secret: 'dbc33e678f', saveUninitialized: true, resave: false, cookie: { maxAge: 3600000 } })); // setup json body parser const bodyParser = require('body-parser'); app.use(bodyParser.json()); // setup logger // const logger = require('utils/request-logger'); // setup express request/response logger const log4js = require('log4js'); log4js.configure('./config/log4js_config.json'); // const log4jsLogger = log4js.getLogger('express-project'); // const fileTransport = logger.fileTransport; // const expressWinston = require('winston-express-middleware'); // expressWinston.requestWhitelist.push('session'); // expressWinston.requestWhitelist.push('body'); // expressWinston.responseWhitelist.push('session'); // expressWinston.responseWhitelist.push('body'); // app.use(expressWinston.logger({ // transports: [fileTransport], // meta: true, // optional: control whether you want to log the meta data about the request (default to true) // msg: "phoenix-partner - :: ## {{req.uid}} - {{req.id}} ## HTTP {{req.method}} {{req.url}} ", // //msg: "SID:[{{req.session.id}}] UID:[{{req.id}}] HTTP {{req.method}} {{req.url}} ", //request-body:{{JSON.stringify(req.body)}}", // -- response-body:{{JSON.stringify(res.body)}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}" // expressFormat: false, // Use the default Express/morgan request formatting, with the same colors. Enabling this will override any msg and colorStatus if true. Will only output colors on transports with colorize set to true // colorStatus: true, // Color the status code, using the Express/morgan color palette (default green, 3XX cyan, 4XX yellow, 5XX red). Will not be recognized if expressFormat is true // ignoreRoute: function (req, res) { return false; }, // optional: allows to skip some log messages based on request and/or response, // customLogger: log4jsLogger, // filterOutList: ['dropdown', 'loggedin', 'query-table', 'query-last-package-number', '.png', '.woff', '.ttf', 'jquery.nanoscroller', 'favicon.ico'], // not log any messages that have one of these words // noExportData: true, // remove resultData in case message has '/export' // noHeader: true, // not log headers if true // noBody: false // not log body if true // })); // setup passport const passport = require('utils/passport-func')(); const flash = require('connect-flash'); app.use(flash()); app.use(passport.initialize()); app.use(passport.session()); app.use(function (req, res, next) { // add generated request-id to session //req.session.reqId = req.id; // Enable CORS // res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Credentials", "true"); res.header("Access-Control-Allow-Origin", req.headers.origin); res.header("Access-Control-Allow-Methods", "POST, PUT, GET, DELETE, OPTIONS"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); }); // get BE api configs const apiUrlPrefix = conf.get('apiUrlPrefix'); const appPort = conf.get('appPort'); // load controllers let mongoQuery = require('./controllers/mongo-db/index'); app.use('/mongo', mongoQuery); let apiSaleArea = require('./controllers/sale-area/index'); app.use(apiUrlPrefix + '/sale-area', apiSaleArea); let apiLogin = require('./controllers/login/login')(passport); app.use('/', apiLogin); // handle not found app.all('*', function(req, res) { res.status(404); // respond with json res.send({ error: 'Not found' }); }); // // handle errors // app.use(function (err, req, res, next) { // // we may use properties of the error object // // here and next(err) appropriately, or if // // we possibly recovered from the error, simply next(). // res.status(err.status || 500).send(err.message || err); // }); // winston-express-middleware errorLogger makes sense AFTER the router. // app.use(expressWinston.errorLogger({ // transports: [fileTransport] // })); app.listen(appPort, function () { console.log('SMAF-Partner Backend app listening on port ' + appPort + '!'); });