This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
89 lines (71 loc) · 1.79 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
82
83
84
85
86
87
88
89
var express = require('express');
var morgan = require('morgan');
var compress = require('compression')();
var bodyParser = require('body-parser');
var UglifyJS = require('uglify-js');
var CleanCSS = require('clean-css');
var errorhandler = require('errorhandler');
var api = express();
api.use(morgan('dev'));
api.use(compress);
api.use(bodyParser({ limit: '1mb' }));
api.use(errorhandler());
/**
* CORS support.
*/
api.all('*', function(req, res, next){
if (!req.get('Origin')) {
return next();
}
// use "*" here to accept any origin
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET, POST');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
if ('OPTIONS' === req.method) {
return res.send(200);
}
next();
});
api.post('/javascript/', function (req, res) {
if (!req.param('code')) {
res.send(404, ':(');
}
try {
output = UglifyJS.minify(req.param('code'), {
fromString: true,
warnings: true,
compress: {
warnings: true
}
});
} catch (error) {
// users don't need to see filestructure of server
delete error.stack;
res.json(500, error);
}
res.send(output);
});
api.post('/css/', function (req, res) {
if (!req.param('code')) {
res.send(404, ':(');
}
var output = {
code: new CleanCSS().minify(req.param('code'))
};
res.send(output);
});
api.post('/gz/:fileName', function (req, res) {
compress(req, res, function (error) {
if (error) {
res.json(error);
}
res.attachment(req.param('fileName'));
res.end(req.param('code'), 'utf8');
});
});
api.all('*', function (req, res) {
res.send(404);
});
api.listen(3000, function() {
console.log('Server listening on port 3000');
});