-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (59 loc) · 2.27 KB
/
app.js
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
/*
* EXPRESS CONFIGURATION FILE
*/
'use strict'
const express = require('express')
const compression = require('compression');
const bodyParser = require('body-parser');
const config = require('./config')
const app = express()
app.use(compression());
const serviceEmail = require('./services/email')
const api = require ('./routes')
const path = require('path')
const allowedOrigins = config.allowedOrigins;
function setCrossDomain(req, res, next) {
//instead of * you can define ONLY the sources that we allow.
//res.header('Access-Control-Allow-Origin', '*');
const origin = req.headers.origin;
if (allowedOrigins.includes(origin) || req.method === 'GET' || req.method === 'HEAD') {
res.header('Access-Control-Allow-Origin', origin);
res.header('Access-Control-Allow-Methods', 'HEAD,GET,PUT,POST,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Access-Control-Allow-Origin, Accept, Accept-Language, Origin, User-Agent, x-api-key');
next();
}else{
//send email
const clientIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const requestInfo = {
method: req.method,
url: req.url,
headers: req.headers,
origin: origin,
body: req.body, // Asegúrate de que el middleware para parsear el cuerpo ya haya sido usado
ip: clientIp,
params: req.params,
query: req.query,
};
if(req.url.indexOf('.well-known/private-click-measurement/report-attribution') === -1){
try {
serviceEmail.sendMailControlCall(requestInfo)
} catch (emailError) {
console.log('Fail sending email');
}
}
res.status(401).json({ error: 'Origin not allowed' });
}
}
app.use(bodyParser.urlencoded({limit: '50mb', extended: false}))
app.use(bodyParser.json({limit: '50mb'}))
app.use(setCrossDomain);
// use the forward slash with the module api api folder created routes
app.use('/api',api)
app.use('/apidoc',express.static('apidoc', {'index': ['index.html']}))
//ruta angular, poner carpeta dist publica
app.use(express.static(path.join(__dirname, 'dist')));
// Send all other requests to the Angular app
app.get('*', function (req, res, next) {
res.sendFile('dist/index.html', { root: __dirname });
});
module.exports = app