diff --git a/components/status.js b/components/status.js index 78a860c..b1c79ca 100644 --- a/components/status.js +++ b/components/status.js @@ -1,10 +1,8 @@ -const express = require('express'), - router = express.Router() - router.get('/v1/manager/status', (req, res) => { // * Ejemplo de Conx. a MySQL + // let conn = db.sql("test", "SELECT * FROM users") // conn.then(data =>{ // functions["alert"].conlog(data) diff --git a/core/modules/express.js b/core/modules/express.js new file mode 100644 index 0000000..81ddbdc --- /dev/null +++ b/core/modules/express.js @@ -0,0 +1,7 @@ +exports.jsonErrorHandler = async (err, req, res, next) => { + const { randomUUID } = require('crypto'), + idreq = randomUUID() + log4j.log("warn", `IP ${req.headers['x-forwarded-for'] || req.socket.remoteAddress } :: Method ${ req.method } :: Endpoint ${req.originalUrl} :: IDREQ ${idreq} :: Se enviaron datos que no estan formateados en JSON`) + res.status(400).json({ msg: "Existe un inconveniente en la solicitud", idreq: idreq}) +} + diff --git a/core/server.js b/core/server.js index 6f596d3..9bdfe4e 100644 --- a/core/server.js +++ b/core/server.js @@ -1,13 +1,12 @@ -const express = require("express"), - exsrv = express(), +global.express = require("express") +global.router = express.Router() + +const exsrv = express(), helmet = require('helmet'), fs = require('fs'), cors = require('cors'), - jsonErrorHandler = async (err, req, res, next) => { - log4j.log("warn", `IP Origen: ${req.headers['x-forwarded-for'] || req.socket.remoteAddress }, Endpoint: ${req.originalUrl} | Se enviaron datos que no estan formateados en JSON`) - res.json({ msg : "Se enviaron datos que no estan formateados en JSON" }); - } - + { jsonErrorHandler } = require("../core/modules/express") + global.functions = [] let dirFunc = require("path").join(__dirname, "../functions/"), @@ -16,29 +15,64 @@ const express = require("express"), functions[r.replace(".js", "")] = require(dirFunc + "/" + r); }) - + let dirApis = require("path").join(__dirname, "../components/"), + apiFiles = fs.readdirSync(dirApis); + apiFiles.forEach(r =>{ + exsrv.use(require(dirApis + "/" + r)); + + }) exsrv.use(express.json()) - exsrv.use(helmet()) exsrv.use(jsonErrorHandler) + exsrv.use(helmet()) exsrv.use(cors({ origin: '*' })); - let dirApis = require("path").join(__dirname, "../components/"), - apiFiles = fs.readdirSync(dirApis); - apiFiles.forEach(r =>{ - exsrv.use(require(dirApis + "/" + r)); - }) + exsrv.get('/methods', (req, res) => { + let methodstoShow = [] + function space(x) { + var res = ''; + while(x--) res += ' '; + return res; + } + + function listRoutes(){ + for (var i = 0; i < arguments.length; i++) { + if(arguments[i].stack instanceof Array){ + arguments[i].stack.forEach(function(a){ + var route = a.route; + if(route){ + route.stack.forEach(function(r){ + var method = r.method.toUpperCase(); + // console.log(method,space(8 - method.length),route.path); + + methodstoShow.push({ + method: method, + uri: route.path + + }) - exsrv.all('*', (req, res, next) => { - log4j.log("warn", `IP Origen: ${req.headers['x-forwarded-for'] || req.socket.remoteAddress }, Endpoint: ${req.originalUrl} | No se puede resolver el metodo `) + }) + } + }); + } + } + } + + listRoutes(router) + + res.status(400).json({ routes: methodstoShow }) + + }) + + exsrv.all('*', (req, res, next) => { + log4j.log("warn", `IP ${req.headers['x-forwarded-for'] || req.socket.remoteAddress } :: Method ${ req.method } :: Endpoint ${req.originalUrl} :: No se puede resolver el metodo `) res.status(404).json({ msg: `No se puede resolver el metodo ${req.originalUrl}` }); - }); module.exports = exsrv \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index eb34b85..38d0613 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "monoIntegrator", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "monoIntegrator", - "version": "1.0.0", + "version": "1.1.0", "license": "Apache-2.0", "dependencies": { "cors": "^2.8.5",