-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-express.js
61 lines (45 loc) · 1.22 KB
/
custom-express.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require('express');
const consign = require('consign');
const expressValidator = require('express-validator');
module.exports = function(){
const app = express();
app.set('view engine', 'ejs');
// Plugin do express
// Middleware
// Executa para cada request
app.use(expressValidator())
app.use((request, response, next) => {
response.header('Access-Control-Allow-Origin', '*')
next()
})
consign()
.include('./routes')
.then('./config')
.then('./repository')
.into(app);
app.use(express.static('./node_modules/bootstrap/dist/'));
app.use(express.static('./public/'));
// (request, response, next) -> tratador de requests padrão
// (request, response, next) -> tratador de requests com erro padrão
app.use((erro, request, response, next) => {
console.error(erro)
next(erro)
})
app.use((erro, request, response, next) => {
response.status(500)
response.format({
html: () => {
response.render('erros/500', {erro})
}
,json: () => {
response.json({erro: erro})
}
})
})
app.use((request, response, next) => {
response
.status(404)
.render('erros/404')
})
return app;
}