forked from tripviss/image-resizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
92 lines (78 loc) · 2.23 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
'use strict';
// Error Handling - Sentry
if (process.env.SENTRY_DSN) {
console.log('Using sentry DSN', process.env.SENTRY_DSN);
const raven = require('raven');
const client = new raven.Client(process.env.SENTRY_DSN);
client.patchGlobal();
}
const express = require('express');
const imageResizer = require('.');
const app = express();
const Img = imageResizer.img;
const env = imageResizer.env;
const streams = imageResizer.streams;
app.directory = __dirname;
imageResizer.expressConfig(app);
// Error Handling - Sentry
if (process.env.SENTRY_DSN) {
const raven = require('raven');
app.use(raven.middleware.express.requestHandler(process.env.SENTRY_DSN));
}
app.get('/favicon.ico', function (request, response) {
response.sendStatus(404);
});
// Show supported modifiers
app.get('/modifiers.json', function (request, response) {
response.json(imageResizer.modifiers);
});
if (env.development) {
// Show a test page of the image options
app.get('/test-page', function (request, response) {
response.render('index.html');
});
// Show the environment variables and their current values
app.get('/env', function (request, response) {
response.json(env);
});
}
// GET images
app.get('/*?', function (req, res, next) {
if (req.path === '/') return next();
const image = new Img(req);
image.getFile()
.pipe(new streams.identify())
.pipe(new streams.normalize())
.pipe(new streams.resize({
allowUpscaling:
req.query.upscale &&
req.query.upscale !== '0' &&
req.query.upscale !== 'false',
}))
.pipe(new streams.filter())
.pipe(new streams.optimize())
.pipe(streams.response(req, res));
});
// Error Handling - Sentry
if (process.env.SENTRY_DSN) {
const raven = require('raven');
app.use(raven.middleware.express.errorHandler(process.env.SENTRY_DSN));
}
// Error Handling - 404
app.use((req, res, next) => {
const err = new Error('Not found');
err.status = 404;
next(err);
});
// Error Handling - JSON errors
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({
error: err.message,
stack: err.stack,
});
});
const port = app.get('port');
app.listen(port, function () {
console.log('Listening on port ', port);
});