From a1ae65dc577a85fc2a0ecdf7198ddad677b06a20 Mon Sep 17 00:00:00 2001 From: Richard Palacios G Date: Wed, 3 Nov 2021 16:07:40 -0500 Subject: [PATCH] feat: add cors * to manage cross-origin resources (#32) add cors * configuration to API close #33 --- nodepop/app.js | 22 +++++++---------- nodepop/middleware/index.js | 47 +++++++++++++++++++------------------ 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/nodepop/app.js b/nodepop/app.js index 1f20327..5ffffcd 100755 --- a/nodepop/app.js +++ b/nodepop/app.js @@ -1,13 +1,12 @@ -var createError = require("http-errors"); -var express = require("express"); -var path = require("path"); -var cookieParser = require("cookie-parser"); -var logger = require("morgan"); -const { error404Handler,generalErrorHandler } = require("./middleware"); +const express = require("express"); +const path = require("path"); +const cookieParser = require("cookie-parser"); +const logger = require("morgan"); +const { error404Handler, generalErrorHandler } = require("./middleware"); const routes = require("./routes"); const config = require("config"); - -var app = express(); +const cors = require("cors"); +const app = express(); // view engine setup app.set("views", path.join(__dirname, "views")); @@ -20,21 +19,18 @@ app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, "public"))); +app.use(cors); /** * Conexión a la base de datos */ require("./services/mongoDB"); -// require("./apiServices/anuncios/model"); -// require("./apiServices/anuncios/controller"); //TODO: add documentation with swagger https://www.youtube.com/watch?v=S8kmHtQeflo&t=859s /** * Rutas de mi API */ -// app.use("/api/anuncios", require("./routes/api/routes")); -// app.use("/api/", routes); app.use(config.get("App.restApiEndpoint.version1AnunciosPath"), routes); // Variables globales para vistas @@ -43,7 +39,7 @@ app.locals.title = "NodePop"; /** * Rutas de mi aplicación web */ -app.use("/",require("./routes/index")); +app.use("/", require("./routes/index")); app.use(error404Handler); // catch 404 and forward to error handler app.use(generalErrorHandler); // error handler diff --git a/nodepop/middleware/index.js b/nodepop/middleware/index.js index d6cdbb7..9204ed8 100644 --- a/nodepop/middleware/index.js +++ b/nodepop/middleware/index.js @@ -1,31 +1,32 @@ "use strict"; +const createError = require("http-errors"); module.exports.error404Handler = function (req, res, next) { - next(createError(404)); + next(createError(404)); }; -module.exports.generalErrorHandler = function(err, req, res, next) { - // comprueba error de validación - if (err.array) { - err.status = 422; - const errInfo = err.array({ onlyFirstError: true })[0]; - err.message = isAPI(req) - ? {message: "Not valid", errors: err.mapped()} - : `Not valid - ${errInfo.param} ${errInfo.msg}`; - } - res.status(err.status || 500); - if(isAPI(req)) { - res.json({ success: false, error: err.message}); - return; - } - // set locals, only providing error in development - res.locals.message = err.message; - res.locals.error = req.app.get("env") === "development" ? err : {}; - // render the error page - res.render("error"); -}; +module.exports.generalErrorHandler = function (err, req, res, next) { + // comprueba error de validación + if (err.array) { + err.status = 422; + const errInfo = err.array({ onlyFirstError: true })[0]; + err.message = isAPI(req) + ? { message: "Not valid", errors: err.mapped() } + : `Not valid - ${errInfo.param} ${errInfo.msg}`; + } + res.status(err.status || 500); + if (isAPI(req)) { + res.json({ success: false, error: err.message }); + return; + } + // set locals, only providing error in development + res.locals.message = err.message; + res.locals.error = req.app.get("env") === "development" ? err : {}; + // render the error page + res.render("error"); +}; // función para saber si es una petición a un API function isAPI(req) { - return req.originalUrl.indexOf("/api") === 0; -} \ No newline at end of file + return req.originalUrl.indexOf("/api") === 0; +}