-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (69 loc) · 1.72 KB
/
index.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
var _ = require('underscore'),
minify = require('clean-css'),
express = require('express'),
app = express();
app.use(express.bodyParser());
// Simple logger
app.use(function (request, response, next) {
console.log('%s %s', request.method, request.url);
next();
});
// Handle uncaughtException
process.on('uncaughtException', function (error) {
exit('Error: ' + error.message);
});
var exit = function (message) {
if (message) {
console.log(message);
}
console.log('Exiting...');
process.exit(1);
};
app.options('/', function (request, response) {
// CORS support
response.set('Access-Control-Allow-Origin', '*');
response.set('Access-Control-Allow-Methods', 'OPTIONS,POST');
response.set('Access-Control-Allow-Headers', 'Content-Type');
// The block definition
response.send({
name: "Minify CSS",
description: "Compresses and minifies CSS.",
inputs: [{
name: "css",
type: "string",
description: "CSS content."
}],
outputs: [{
name: "minified",
type: "string",
description: "Minified CSS."
}]
});
});
app.post('/', function (request, response) {
if (!_.has(request.body, 'inputs') && isArray(request.body.inputs)) {
exit('WebPipe "input" is missing or formatted incorrectly.');
}
var inputs = request.body.inputs[0];
var outputs = {
outputs: []
};
// Verify POST keys exist
if (!_.has(inputs, 'css')) {
exit('Input is missing required "css" content.');
}
var minified = minify.process(inputs.css);
if (!minified) {
console.log('Failed to copmress CSS.');
response.send(500);
} else {
outputs.outputs.push({
minified: minified
});
response.json(outputs);
}
});
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('Listening on ' + port);
});