Skip to content

Commit

Permalink
feat: add cors * to manage cross-origin resources (#32)
Browse files Browse the repository at this point in the history
add cors * configuration to API close #33
  • Loading branch information
rpalaciosg authored Nov 3, 2021
1 parent 80228ed commit a1ae65d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
22 changes: 9 additions & 13 deletions nodepop/app.js
Original file line number Diff line number Diff line change
@@ -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"));
Expand All @@ -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
Expand All @@ -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

Expand Down
47 changes: 24 additions & 23 deletions nodepop/middleware/index.js
Original file line number Diff line number Diff line change
@@ -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;
}
return req.originalUrl.indexOf("/api") === 0;
}

0 comments on commit a1ae65d

Please sign in to comment.