Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
README com lista básica de dependências, renomeado para .md.
Arquivo básico com webservice para uglify, emitindo erro na resposta http em caso de falha
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Dependências: | ||
------------- | ||
|
||
*uglify-js* | ||
*connect-form* | ||
*express* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var express = require('express') | ||
, form = require('connect-form') | ||
, app = module.exports = express.createServer( | ||
form({ keepExtensions: true }) | ||
) | ||
, uglify = require('uglify-js') | ||
, jsp = uglify.parser | ||
, pro = uglify.uglify | ||
, fs = require('fs'); | ||
|
||
app.get('/', function(req, res){ | ||
res.send('<form method="post" enctype="multipart/form-data">' | ||
+ '<p><textarea name="fileContent"></textarea></p>' | ||
+ '<p><input type="submit" value="send contents" /></p>' | ||
+ '</form>'); | ||
}); | ||
|
||
app.post('/', function(req, res, next){ | ||
|
||
// connect-form adds the req.form object | ||
// we can (optionally) define onComplete, passing | ||
// the exception (if any) fields parsed, and files parsed | ||
req.form.complete(function(err, fields, files){ | ||
if (err) { | ||
next(err); | ||
} else { | ||
fields.fileContent = fields.fileContent || ''; | ||
var orig_code = fields.fileContent; | ||
|
||
|
||
try { | ||
var ast = jsp.parse(orig_code); // parse code and get the initial AST | ||
ast = pro.ast_mangle(ast); // get a new AST with mangled names | ||
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations | ||
var final_code = pro.gen_code(ast); // compressed code here | ||
} catch (e) { | ||
Error('not valid'); | ||
res.status(400); | ||
res.send('error: ' + e.message); | ||
return; | ||
} | ||
/* | ||
* other options in https://github.com/mishoo/UglifyJS | ||
*/ | ||
res.charset = 'utf-8'; | ||
res.header('Content-Type', 'application/javascript'); | ||
res.send(final_code); // now this sends the final code to screen | ||
} | ||
}); | ||
}); | ||
|
||
app.get('/upload', function(req, res){ | ||
res.send('<form method="post" enctype="multipart/form-data">' | ||
+ '<p>File: <input type="file" name="file" /></p>' | ||
+ '<p><input type="submit" value="Upload" /></p>' | ||
+ '</form>'); | ||
}); | ||
|
||
app.post('/upload', function(req, res, next){ | ||
|
||
// connect-form adds the req.form object | ||
// we can (optionally) define onComplete, passing | ||
// the exception (if any) fields parsed, and files parsed | ||
req.form.complete(function(err, fields, files){ | ||
if (err) { | ||
next(err); | ||
} else { | ||
console.log('\nuploaded %s to %s' | ||
, files.file.filename | ||
, files.file.path | ||
); | ||
|
||
var orig_code = fs.readFileSync(files.file.path, 'utf-8'); | ||
|
||
var ast = jsp.parse(orig_code); // parse code and get the initial AST | ||
ast = pro.ast_mangle(ast); // get a new AST with mangled names | ||
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations | ||
var final_code = pro.gen_code(ast); // compressed code here | ||
|
||
/* | ||
* other options in https://github.com/mishoo/UglifyJS | ||
*/ | ||
res.charset = 'utf-8'; | ||
res.contentType(files.file.path); | ||
res.send(final_code); // now this sends the final code to screen | ||
} | ||
}); | ||
|
||
// We can add listeners for several form | ||
// events such as "progress" | ||
req.form.on('progress', function(bytesReceived, bytesExpected){ | ||
var percent = (bytesReceived / bytesExpected * 100) | 0; | ||
process.stdout.write('Uploading: %' + percent + '\r'); | ||
}); | ||
}); | ||
|
||
app.listen(3000); | ||
console.log("Express server listening on port %d in %s mode", | ||
app.address().port, app.settings.env); |