-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cors * to manage cross-origin resources (#32)
add cors * configuration to API close #33
- Loading branch information
1 parent
80228ed
commit a1ae65d
Showing
2 changed files
with
33 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |