﻿const path = require("path");
const config = require("config");
const express = require("express");
const bodyParser = require("body-parser");
const favicon = require("serve-favicon");

const routes = require("./routes");
const crawler = require("./controllers/crawler");
const dbhelper = require("./controllers/dbhelper");
const assetsMap = require("./public/assets/assets.json");

// init db.
dbhelper.start(() =>
{
    dbhelper.connect((err) =>
    {
        if (err)
        {
            console.error("Database Server not started, some features will be shut down.");
        }
        else
        {
            if (config.crawler.enabled)
            {
                crawler.start();
            }
        }
    });
});

// init express.
const app = express();

// view engine setup.
app.set("views", __dirname + "/views/");
app.set("view engine", "jade");

// favicon setup.
app.use(favicon(__dirname + "/public/favicon.ico"));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// router setup.
app.use("/", routes);

// static file setup.
app.use(express.static(__dirname + "/public/", {
    maxAge: 2592000000
}));

// assets map setup.
app.locals.map = assetsMap;

// catch 404 and forward to global error handler.
app.use((req, res, next) =>
{
    const err = new Error("404 Not Found");
    err.status = 404;
    next(err);
});

// global error handler.
app.use((err, req, res, next) =>
{
    res.status(err.status || 500).render("error_404", { map: app.locals.map });
});

module.exports = app;

@@ -1,9 +1,26 @@
 %EF%BB%BF
+%22use strict%22;%0D%0A%0D%0A
 const pa
@@ -434,16 +434,19 @@
 .start((
+err
 ) =%3E%0D%0A%7B%0D
@@ -442,24 +442,198 @@
 err) =%3E%0D%0A%7B%0D%0A
+    let msg = %22%5CnDatabase Server not started, some features will be shut down.%22;%0D%0A    if (err)%0D%0A    %7B%0D%0A        console.error(err.message + msg);%0D%0A    %7D%0D%0A    else%0D%0A    %7B%0D%0A    
     dbhelper
@@ -651,27 +651,35 @@
 rr) =%3E%0D%0A    
+ 
-%7B%0D%0A
+   %7B%0D%0A    
         if (
@@ -684,35 +684,43 @@
  (err)%0D%0A        
+  
+  
 %7B%0D%0A
+    
             cons
@@ -733,86 +733,34 @@
 ror(
-%22Database Server not started, some features will be shut down.%22);%0D%0A        %7D%0D%0A
+msg);%0D%0A            %7D%0D%0A    
     
@@ -769,35 +769,43 @@
   else%0D%0A        
-%7B%0D%0A
+    %7B%0D%0A    
             if (
@@ -833,35 +833,43 @@
 d)%0D%0A            
-%7B%0D%0A
+    %7B%0D%0A    
                 
@@ -890,32 +890,36 @@
 );%0D%0A            
+ 
+   
 %7D%0D%0A        %7D%0D%0A  
@@ -917,20 +917,35 @@
     
+  
+  
 %7D%0D%0A    
+  
+  
 %7D);%0D%0A
+    %7D%0D%0A
 %7D);%0D

add err handling