Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
134 lines (105 loc) · 4.17 KB
/
Copy pathapp.js
File metadata and controls
134 lines (105 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const morgan = require('morgan');
const cacheControl = require('express-cache-controller');
const stylus = require('stylus');
const hbs = require('express-handlebars');
const dotenv = require('dotenv');
const dotenvExpand = require('dotenv-expand');
const myEnv = dotenv.config(); dotenvExpand(myEnv)
const indexRouter = require('./routes/index');
const lib = require("./lib/lib");
const app = express();
const helpers = require("./lib/helpers.js").helpers;
function initApp(appLocals) {
// console.log("appLocals general",appLocals.general);
// console.log("appLocals buscadores",appLocals.buscadores);
// console.log("appLocals notas",appLocals.notas);
// handlebars setup
app.engine('.hbs', hbs({
extname: 'hbs',
defaultLayout: 'layout',
layoutsDir: path.join(__dirname, appLocals.general.views[0].staging),
partialsDir : [
// path to your partials
path.join(__dirname, 'views/partials'),
path.join(__dirname, appLocals.general.partials[0].staging),
],
helpers: helpers,
}));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// log only 4xx and 5xx responses to console
app.use(morgan('short', {
skip: function (req, res) { return (res.statusCode < 400 && (req.headers.accept && req.headers.accept.indexOf("html") == -1 )) }
}))
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
console.log("stylus path",appLocals.general.style_path[0].staging);
app.use(stylus.middleware(
{
"serve": true,
"dest": (appLocals.general.extra_static_path) ? path.join(__dirname, appLocals.general.extra_static_path[0].staging): "public",
"src": path.join(__dirname, appLocals.general.style_path[0].staging),
"force": true,
"linenos": false,
}
));
const staticOptions = {
index:false,
cacheControl: true,
maxAge: 60000000
};
app.use("/", express.static(path.join(__dirname, 'public'), staticOptions));
if (appLocals.general.extra_static_path) {
app.use("/extra", express.static(path.join(__dirname, appLocals.general.extra_static_path[0].staging), staticOptions));
}
// Bootstrap 4 and libraries
app.use('/jQuery', express.static(__dirname + '/node_modules/jquery/dist/',staticOptions));
app.use('/bootstrap', express.static(__dirname + '/node_modules/bootstrap/dist/',staticOptions));
app.use('/tiza', express.static(__dirname + '/node_modules/tiza',staticOptions));
app.use('/datatables', express.static(__dirname + '/node_modules/datatables.net/js',staticOptions));
app.use('/datatables-styles', express.static(__dirname + '/node_modules/datatables.net-dt/css',staticOptions));
app.use(cacheControl({
// public: true,
noCache: true
}
));
app.use('/', indexRouter);
console.log("App started, server listening. Env",helpers.env());
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
//Don't show trace for 404 errors
if (err.message!="Not Found") {
// set locals, only providing error in development
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.locals.message = err.message;
console.error("/!\\ QuienEsQuien.Wiki APP Error at URL: ",req.url);
console.error("/!\\ QuienEsQuien.Wiki APP Error: ",err);
}
res.cacheControl = {
noStore: true
}
// render the error page
res.status(err.status || 500);
res.render('error', { error: true, current_url: req.url });
});
//Last resource error handler.
app.use(function(err, req, res, next) {
res.status(404);
res.json({
en: "We will recover from this too. Please reload the page. If this message repeats, please let us know.",
es: "Nos vamos a recuperar de esta. Por favor refresque la página. Si este mensaje se repite por favor infórmenos."
});
});
}
lib.loadSettings(app,initApp);
module.exports = app;