-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (81 loc) · 2.17 KB
/
index.js
File metadata and controls
111 lines (81 loc) · 2.17 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Module Dependencies
*/
import express from 'express';
import bodyParser from 'body-parser';
import expressValidator from 'express-validator';
import errorHandler from 'errorhandler';
// Additional Modules
import config from 'config';
import routes from 'routes';
/**
* Create Express app
*/
let app = express();
/**
* Express Configuration and Setup
*/
app.locals.application = config.name;
app.locals.version = config.version;
app.locals.description = config.description;
app.locals.author = config.author;
app.locals.keywords = config.keywords;
app.locals.custom = config.custom;
app.locals.environment = config.environment;
// Port to listen on
app.set('port', config.port);
// Body parsing middleware supporting
app.use(bodyParser.json());
// CORS
app.use(function(request, result, next) {
result.setHeader('Access-Control-Allow-Origin', '*');
next();
});
// Validation Middleware
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
const namespace = param.split('.');
let formParam = namespace.shift();
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
field : formParam,
message : msg,
value : value
};
}
}));
/**
* Routes/Routing
*/
app.use('/api', routes);
/**
* Error Handling
*/
// Handle 404 Errors
app.use(function (req, res, next) {
res.status(404);
res.send('Endpoint not found!');
return;
});
if (app.get('environment') !== 'production') {
// Final error catch-all just in case
app.use(errorHandler());
}
process.on('uncaughtException', function(err) {
console.log('Node alert an uncaught exception:');
console.error(err);
});
// Production 500 error handler (no stacktraces leaked to public!)
app.use(function (err, req, res, next) {
var status = err.status || 500;
res.status(status).send({
'code': status,
'error': 'Internal Server Error',
'message': app.get('environment') === 'production' ? 'Ocorreu um erro inesperado.' : err.stack
});
return;
});
app.listen(config.port);
console.log('Express server started on port 3000.');