const express = require('express'), cors = require("cors"), app = express(), session = require('express-session'), bodyParser = require('body-parser'), cookieParser = require('cookie-parser'), helmet = require('helmet'), log = require("color-logs")(true, true, "Nodezzarella app"), fs = require("fs"), promise = require('promise'); log.info("Application started and now preparing"); var corsOptions = { "origin": "*", "Access-Control-Allow-Origin": "*", "methods": "GET,HEAD,PUT,PATCH,POST,DELETE", "preflightContinue": false }; global.config = require("./config.json"); class Nodezzarella { constructor(rootPath){ this.getDBType().then((configDB) => { if(!configDB.flatFile){ var db = require('./lib/db/'+config.db+'/db'); new db().connectDB(configDB).then((connexion) => { global.dbConnexion = connexion; global.dbConf = configDB; this.run(); }); } else{ this.run(); } }).catch((err) => { new Error("Nodezzarella can't initialize"); }); } getDBType(){ return new Promise((resolve, reject) => { fs.access("./lib/db/"+config.db+"/config.json", fs.constants.F_OK || fs.constants.R_OK, (error) => { if(error){ reject(new Error("File not exist")); } else{ resolve(require("./lib/db/"+config.db+"/config.json")); } }); }); } run(){ var routes = require("./lib/router"); log.info("HTTP server listening on port", config.appPort); log.info("Application ready"); app.set('trust proxy', 1); app.use(cookieParser()); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(session({ secret: 'Nodezzarella', resave: true, saveUninitialized: true, cookie: { secure: false, maxAge: 3600*24*31 } })) app.use(config.webroot || "/", routes); app.use(function(req, res, next) { res.status(404); res.send("404 not found"); return; }); /** * Listen to http://0.0.0.0:port */ app.disable('x-powered-by'); app.listen(config.appPort || 8888); } } new Nodezzarella("/");