-
Notifications
You must be signed in to change notification settings - Fork 302
/
app.js
177 lines (145 loc) · 3.71 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { cacheMiddleware, NotFound } from './utils.js';
import CONFIG from './config.loader.js';
global.CONFIG = CONFIG;
console.log("");
console.log("Starting Iframely...");
console.log("Base URL for embeds that require hosted renders:", CONFIG.baseAppUrl);
if (!CONFIG.baseAppUrl) {
console.warn('Warning: CONFIG.baseAppUrl not set, default value used');
}
import express from 'express';
import * as jsonxml from 'jsontoxml';
const app = express();
export default app;
app.set('view engine', 'ejs');
if (CONFIG.allowedOrigins) {
app.use(function(req, res, next) {
var origin = req.headers["origin"];
if (origin) {
if (CONFIG.allowedOrigins.indexOf('*') > -1) {
res.setHeader('Access-Control-Allow-Origin', '*');
} else {
if (CONFIG.allowedOrigins.indexOf(origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', origin);
}
}
}
next();
});
}
app.disable( 'x-powered-by' );
app.use(function(req, res, next) {
res.setHeader('X-Powered-By', 'Iframely');
next();
});
app.use(cacheMiddleware);
import apiViews from './modules/api/views.js';
import debugViews from './modules/debug/views.js';
apiViews(app);
debugViews(app);
if (CONFIG.tests) {
const testViews = await import('./modules/tests-ui/views.js');
testViews.default(app);
}
app.use(logErrors);
app.use(errorHandler);
function logErrors(err, req, res, next) {
if (CONFIG.RICH_LOG_ENABLED) {
console.error(err.stack);
} else {
console.log(err.message);
}
next(err);
}
function respondWithError(req, res, code, msg, messages) {
var err = {
error: {
source: 'iframely',
code: code,
message: msg
}
};
if (messages) {
err.error.messages = messages;
}
var ttl;
if (code === 404) {
ttl = CONFIG.CACHE_TTL_PAGE_404;
} else if (code === 408) {
ttl = CONFIG.CACHE_TTL_PAGE_TIMEOUT;
} else {
ttl = CONFIG.CACHE_TTL_PAGE_OTHER_ERROR
}
if (req.query.format === 'xml') {
var xmlError = jsonxml(err, {
escape: true,
xmlHeader: {
standalone: true
}
});
res.sendCached('text/xml', xmlError, {
code: code,
ttl: ttl
});
} else {
res.sendJsonCached(err, {
code: code,
ttl: ttl
});
}
}
var proxyErrorCodes = [401, 403, 408];
function errorHandler(err, req, res, next) {
if (err instanceof NotFound) {
respondWithError(req, res, 404, err.message, err.messages);
} else {
var code = err.code || 500;
proxyErrorCodes.map(function(e) {
if (err.message.indexOf(e) > - 1) {
code = e;
}
});
var message = 'Server error';
if (code === 400) {
message = err.message && ('Bad Request: ' + err.message) || 'Bad Request';
}
else if (code === 401) {
message = 'Unauthorized';
// Force 403 to prevent Basic auth popup.
code = 403;
}
else if (code === 403) {
message = 'Forbidden';
}
else if (code === 404) {
message = 'Not found';
}
else if (code === 408) {
message = 'Timeout';
}
else if (code === 410) {
message = 'Gone';
}
else if (code === 415 || code === 417) {
message = err.message || 'Unsupported Media Type';
}
respondWithError(req, res, code, message, err.messages);
}
}
process.on('uncaughtException', function(err) {
if (CONFIG.DEBUG) {
console.log(err.stack);
} else {
console.log(err.message);
}
});
if (process.env.NODE_ENV !== 'test') {
// This code not compatible with 'supertest' (in e2e.js)
// Disabled for tests.
app.use(CONFIG.relativeStaticUrl, express.static('static'));
}
app.get('/', function(req, res) {
res.writeHead(302, { Location: 'http://iframely.com'});
res.end();
});
process.title = "iframely";