Skip to content

Commit

Permalink
Versão 2 boilerplate voltada para api com classe de erros.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpsdantas committed Feb 24, 2019
1 parent 27468ed commit e2f7f8c
Show file tree
Hide file tree
Showing 13 changed files with 798 additions and 187 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
Expand Down
109 changes: 78 additions & 31 deletions config/app.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@
/* importar o módulo do framework express */
const express = require('express');
/*
* @Author: Marcus Dantas
* @Date: 2019-02-03 01:54:51
* @Last Modified by: Marcus Dantas
* @Last Modified time: 2019-02-24 20:04:48
*/

/* importar o módulo do consign */
const consign = require('consign');
const express = require("express");

/* importar o módulo do body-parser */
const bodyParser = require('body-parser');
const consign = require("consign");

/* importar o módulo do express-validator */
const expressValidator = require('express-validator');
const bodyParser = require("body-parser");

/*Importando modulo morgan*/
const morgan = require('morgan');
const expressValidator = require("express-validator");

const morgan = require("morgan");

const mongoose = require("mongoose");

const ResponseErrorBuild = require("../src/utils/erros/ResponseBuildError");

const SimpleNodeLogger = require("simple-node-logger");

const caminhoLog = `${__dirname}/../logs/logs-system.log`;

const opts = {
logFilePath: caminhoLog,
timestampFormat: "YYYY-MM-DD HH:mm:ss.SSS"
};

const log = require("simple-node-logger").createSimpleLogger(opts);

global.log = log;

require("module-alias/register");

/* iniciar o objeto do express */
const app = express();

/* Variaveis de ambiente. */
const env = require('dotenv');
const cors = require('cors')

app.use(cors());

app.use(express.static("./public"));

/* Importando o módulo do mongoose. */
const mongoose = require('mongoose');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(expressValidator());

/* Importando o módulo express-session. */

Expand All @@ -39,31 +64,53 @@ app.set('view engine', 'ejs');
app.set('views', './src/views');
*/

/* configurar o middleware express.static */
app.use(express.static('./public'));
app.use(morgan("dev"));

/* configurar o middleware body-parser */
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
consign()
.include("src/models")
.then("src/routes")
.into(app);

/* configurar o middleware express-validator */
app.use(expressValidator());
mongoose.set("useCreateIndex", true);

/* Setando morgan */
app.use(morgan('dev'));
app.use((err, req, res, next) => {
log.error(
"Arquivo: app.j | Erro catch middleware ",
err,
" | criado em: ",
new Date().toJSON()
);
const responseBuildError = new ResponseErrorBuild(res, err);
responseBuildError.send();
});

/* efetua o autoload das rotas, dos models e dos controllers para o objeto app */
consign().include('src/models')
.then('src/routes')
.then('src/controllers').into(app);

app.use((req, res, next) => {
res.status(404).json({
status: false,
code: 404,
erros: [{
msg: `Nenhuma rota encontrada para ${req.path}`
}],
date: new Date()
});
});

/* Extraindo variaveis de ambiente. */
env.config({ path: './env/dev.env' });
if (process.env.NODE_ENV !== "production") {
require("dotenv").config({ path: "./env/dev.env" });
} else {
require("dotenv").config({ path: "./env/prod.env" });
}

/* Conecta com o banco de dados e lida com problemas de conexão */
mongoose.connect(process.env.DATABASE, { useNewUrlParser: true });
mongoose.connect(process.env.DATABASE, {
useNewUrlParser: true
});

mongoose.Promise = global.Promise; // → Queremos que o mongoose utilize promises ES6
mongoose.connection.on('error',err => {

mongoose.connection.on("error", err => {
console.log(`🙅 🚫 → ${err.message}`);
});

Expand Down
4 changes: 4 additions & 0 deletions config/variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const variables = {
};

module.exports = variables;
19 changes: 19 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es2018",
"allowSyntheticDefaultImports": false,
"baseUrl": "./",
"paths": {
"controllers/*": ["src/controllers/*"],
"models/*": ["src/models/*"],
"middlewares/*": ["src/middlewares/*"],
"routes/*": ["src/routes/*"],
"app/*": ["src/*"],
"utils/*": ["src/utils/*"],
"validators/*": ["src/validators/*"],
"config/*": ["./config/*"]
}
},
"exclude": ["node_modules", "dist"],
"module": "commonjs"
}
Empty file added logs/.gitkeep
Empty file.
Loading

0 comments on commit e2f7f8c

Please sign in to comment.