-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
107 lines (90 loc) · 2.88 KB
/
app.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
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
/**
* InterMine Datasets
*/
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var service = require('./routes/service');
const scheduledAutomaticUpdate = require('./scheduled/automaticUpdate');
var app = express();
// Views Engine Setup: EJS
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
/**
* ============ Middlewares =============
*/
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
// Middleware to delete URL final slash
app.use(function(req, res, next) {
if(req.url.substr(-1) != '/' && req.url.substr(-8) == "registry" && req.url.length > 1){
res.redirect(301, req.url + "/");
} else{
next();
}
});
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(__dirname + '/node_modules/bootstrap/dist'));
app.use(express.static(__dirname + '/node_modules/bootstrap-material-design/dist'));
app.use('/images', express.static(__dirname + '/node_modules/leaflet-search/images'));
//enable CORS
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
//Routes
app.use('/', index);
app.use('/service', service);
// 404 catcher
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
if (req.accepts('html')) {
res.render('404', { url: req.url });
return;
}
// respond with json
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
// Error Handler Middlewares
app.use(function(err, req, res, next) {
// JsonSchema not valid
if (err.name === 'JsonSchemaValidation'){
res.status(400).json({
statusCode: 400,
message: "Malformed JSON. Invalid or Missing Data.",
friendlyMessage: "Please, fill out all the required fields.",
executionTime: new Date().toLocaleString()
});
return;
}
// JSON is not valid
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
res.status(400).json({
statusCode: 400,
message: "Malformed JSON",
friendlyMessage: "Malformed JSON",
executionTime: new Date().toLocaleString()
});
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.status(err.status || 500);
res.render('error');
});
module.exports = app;