2023-10-04 10:20:50 +00:00
|
|
|
"use strict";
|
2019-04-15 10:13:17 +00:00
|
|
|
|
2023-10-04 10:20:50 +00:00
|
|
|
const express = require("express");
|
2019-04-15 10:13:17 +00:00
|
|
|
//const morgan = require('morgan');
|
2023-10-04 10:20:50 +00:00
|
|
|
const bodyParser = require("body-parser");
|
|
|
|
|
const compress = require("compression");
|
|
|
|
|
const responseTime = require("response-time");
|
|
|
|
|
const methodOverride = require("method-override");
|
|
|
|
|
const cors = require("cors");
|
|
|
|
|
const helmet = require("helmet");
|
|
|
|
|
const passport = require("passport");
|
|
|
|
|
|
|
|
|
|
const config = require("../config");
|
|
|
|
|
const router = require("./router");
|
|
|
|
|
const error = require("../middlewares/error");
|
|
|
|
|
|
|
|
|
|
const deviceCountryMiddleware = require("../middlewares/deviceCountry");
|
2019-04-24 21:01:54 +00:00
|
|
|
|
2019-04-15 15:58:58 +00:00
|
|
|
/**
|
|
|
|
|
* Express instance
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
|
|
// request logging. dev: console | production: file
|
|
|
|
|
//app.use(morgan(logs));
|
|
|
|
|
|
|
|
|
|
// parse body params and attache them to req.body
|
2023-10-04 10:20:50 +00:00
|
|
|
app.use(bodyParser.json({ limit: "5mb" }));
|
|
|
|
|
app.use(
|
|
|
|
|
bodyParser.urlencoded({
|
|
|
|
|
limit: "5mb",
|
|
|
|
|
extended: true,
|
|
|
|
|
})
|
|
|
|
|
);
|
2019-04-15 15:58:58 +00:00
|
|
|
|
|
|
|
|
// set up the response-time middleware
|
|
|
|
|
app.use(responseTime());
|
|
|
|
|
|
|
|
|
|
// gzip compression
|
|
|
|
|
app.use(compress());
|
|
|
|
|
|
|
|
|
|
// lets you use HTTP verbs such as PUT or DELETE
|
|
|
|
|
// in places where the client doesn't support it
|
|
|
|
|
app.use(methodOverride());
|
|
|
|
|
|
|
|
|
|
// secure apps by setting various HTTP headers
|
|
|
|
|
app.use(helmet());
|
|
|
|
|
|
|
|
|
|
// enable CORS - Cross Origin Resource Sharing
|
2023-10-04 10:20:50 +00:00
|
|
|
//var allowedOrigins = ['http://localhost:8080', 'http://127.0.0.1:8080', 'https://adminapp2.loquedeverdadimporta.org'];
|
2019-08-18 21:15:34 +00:00
|
|
|
|
2019-09-13 14:27:43 +00:00
|
|
|
// enable CORS - Cross Origin Resource Sharing
|
2023-10-04 10:20:50 +00:00
|
|
|
app.use(
|
|
|
|
|
cors({
|
|
|
|
|
exposedHeaders: [
|
|
|
|
|
"X-Country",
|
|
|
|
|
"Content-Disposition",
|
|
|
|
|
"Content-Type",
|
|
|
|
|
"Content-Length",
|
|
|
|
|
"X-Total-Count",
|
|
|
|
|
"Pagination-Count",
|
|
|
|
|
"Pagination-Page",
|
|
|
|
|
"Pagination-Limit",
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
);
|
2019-09-13 14:27:43 +00:00
|
|
|
|
|
|
|
|
/*app.use(cors({
|
2019-09-05 15:09:25 +00:00
|
|
|
origin: function (origin, callback) {
|
|
|
|
|
// allow requests with no origin
|
2019-08-18 21:15:34 +00:00
|
|
|
// (like mobile apps or curl requests)
|
2019-09-05 15:09:25 +00:00
|
|
|
return callback(null, true);
|
2019-09-13 14:27:43 +00:00
|
|
|
|
2019-08-18 21:15:34 +00:00
|
|
|
if (!origin) {
|
|
|
|
|
return callback(null, true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 14:53:33 +00:00
|
|
|
console.log('origin =>', origin);
|
|
|
|
|
|
2019-08-18 21:15:34 +00:00
|
|
|
if (allowedOrigins.indexOf(origin) === -1) {
|
|
|
|
|
var msg = 'The CORS policy for this site does not ' +
|
|
|
|
|
'allow access from the specified Origin.';
|
|
|
|
|
return callback(new Error(msg), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return callback(null, true);
|
|
|
|
|
}
|
2019-09-13 14:27:43 +00:00
|
|
|
}));*/
|
2019-08-18 21:15:34 +00:00
|
|
|
|
|
|
|
|
/*app.use(cors({
|
2019-08-07 13:56:25 +00:00
|
|
|
origin: '*',
|
2019-04-15 15:58:58 +00:00
|
|
|
exposeHeaders: [
|
|
|
|
|
"WWW-Authenticate",
|
2019-04-24 21:01:54 +00:00
|
|
|
"Server-Authorization",
|
|
|
|
|
"Content-Disposition",
|
2019-08-07 13:56:25 +00:00
|
|
|
"Content-Type",
|
2019-04-24 21:01:54 +00:00
|
|
|
"Content-Length"
|
2019-04-15 15:58:58 +00:00
|
|
|
],
|
|
|
|
|
maxAge: 31536000,
|
|
|
|
|
credentials: true,
|
|
|
|
|
allowMethods: [
|
|
|
|
|
"GET",
|
|
|
|
|
"POST",
|
|
|
|
|
"PUT",
|
|
|
|
|
"PATCH",
|
|
|
|
|
"DELETE",
|
|
|
|
|
"OPTIONS",
|
|
|
|
|
"HEAD"
|
|
|
|
|
],
|
|
|
|
|
allowHeaders: [
|
2019-08-18 21:15:34 +00:00
|
|
|
"Access-Control-Allow-Origin",
|
2019-04-15 15:58:58 +00:00
|
|
|
"Content-Type",
|
2019-08-07 13:56:25 +00:00
|
|
|
"X-CSRF-Token",
|
|
|
|
|
"X-Requested-With",
|
|
|
|
|
"Accept",
|
|
|
|
|
"Accept-Version",
|
|
|
|
|
"Content-Length",
|
|
|
|
|
"Content-MD5",
|
|
|
|
|
"Date",
|
|
|
|
|
"X-Api-Version",
|
|
|
|
|
"X-File-Name",
|
2019-04-15 15:58:58 +00:00
|
|
|
"Authorization",
|
|
|
|
|
"X-Frame-Options",
|
2019-08-07 13:56:25 +00:00
|
|
|
"Origin",
|
2019-04-15 15:58:58 +00:00
|
|
|
],
|
2019-08-18 21:15:34 +00:00
|
|
|
}));*/
|
2019-04-15 15:58:58 +00:00
|
|
|
|
2023-10-04 10:20:50 +00:00
|
|
|
app.use(deviceCountryMiddleware.middleware());
|
2019-08-07 13:56:25 +00:00
|
|
|
|
2019-04-15 15:58:58 +00:00
|
|
|
// Access validator
|
|
|
|
|
app.use(passport.initialize());
|
2023-10-04 10:20:50 +00:00
|
|
|
require("./passport");
|
2019-04-15 15:58:58 +00:00
|
|
|
|
2024-09-04 11:39:14 +00:00
|
|
|
// APIDOC
|
|
|
|
|
app.use("/doc", express.static("apidoc"));
|
|
|
|
|
|
2019-04-15 15:58:58 +00:00
|
|
|
// Set routes
|
2023-10-04 10:20:50 +00:00
|
|
|
app.use("/api", router());
|
2019-04-15 15:58:58 +00:00
|
|
|
|
|
|
|
|
// if error is not an instanceOf APIError, convert it.
|
|
|
|
|
app.use(error.converter);
|
|
|
|
|
|
|
|
|
|
// catch 404 and forward to error handler
|
|
|
|
|
app.use(error.notFound);
|
|
|
|
|
|
|
|
|
|
// error handler, send stacktrace only during development
|
|
|
|
|
app.use(error.handler);
|
|
|
|
|
|
2023-10-04 10:20:50 +00:00
|
|
|
module.exports = app;
|