-
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.
Merge pull request #23 from rpalaciosg/refactor-ddd
- Loading branch information
Showing
12 changed files
with
931 additions
and
838 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
nodepop/controllers/AnuncioController.js → nodepop/apiServices/anuncios/controller.js
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
File renamed without changes.
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,74 +1,46 @@ | ||
var createError = require('http-errors'); | ||
var express = require('express'); | ||
var path = require('path'); | ||
var cookieParser = require('cookie-parser'); | ||
var logger = require('morgan'); | ||
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 routes = require("./routes"); | ||
|
||
var app = express(); | ||
|
||
// view engine setup | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'html'); | ||
app.engine('html', require('ejs').__express); | ||
app.set("views", path.join(__dirname, "views")); | ||
app.set("view engine", "html"); | ||
app.engine("html", require("ejs").__express); | ||
|
||
// Middlewares | ||
app.use(logger('dev')); | ||
app.use(logger("dev")); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
app.use(express.static(path.join(__dirname, "public"))); | ||
|
||
/** | ||
* Conexión a la base de datos | ||
*/ | ||
require('./lib/connectDB'); | ||
require('./models/Anuncio'); | ||
require('./controllers/AnuncioController'); | ||
require("./services/mongoDB"); | ||
// require("./apiServices/anuncios/model"); | ||
// require("./apiServices/anuncios/controller"); | ||
|
||
/** | ||
* Rutas de mi API | ||
*/ | ||
app.use('/apiv1/anuncios', require('./routes/apiv1/anuncios')); | ||
// app.use("/apiv1/anuncios", require("./routes/apiv1/routes")); | ||
app.use("/apiv1/", routes); | ||
|
||
// Variables goblales para vistas | ||
app.locals.title = 'NodePop'; | ||
// Variables globales para vistas | ||
app.locals.title = "NodePop"; | ||
|
||
/** | ||
* Rutas de mi aplicación web | ||
*/ | ||
app.use('/', require('./routes/index')); | ||
app.use('/users', require('./routes/users')); | ||
|
||
// catch 404 and forward to error handler | ||
app.use(function(req, res, next) { | ||
next(createError(404)); | ||
}); | ||
|
||
// error handler | ||
app.use(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; | ||
} | ||
app.use("/",require("./routes/index")); | ||
app.use(error404Handler); // catch 404 and forward to error handler | ||
app.use(generalErrorHandler); // error handler | ||
|
||
module.exports = app; |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use strict"; | ||
|
||
module.exports.error404Handler = function (req, res, next) { | ||
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"); | ||
}; | ||
|
||
// función para saber si es una petición a un API | ||
function isAPI(req) { | ||
return req.originalUrl.indexOf("/api") === 0; | ||
} |
Oops, something went wrong.