时间紧,任务重,废话不多说了,直接上代码,uglifyJS中用到的参数可以直接在下面的官方参考链接中查阅
-
uglifyJS参考:uglifyJS2git官方文档;
-
node参考:node官方http解析文档
-
安装node的加密模块:npm install uglify-js
##service.js
const http = require('http');
const queryStr = require('querystring');
const uglifyJS = require('uglify-js');
const hostname = '192.168.31.242';
const port = 8089;
const server = http.createServer((request, response) => {
let body = '';
request.on('error', (err) => {
console.error(err);
response.statusCode = 404;
response.end();
});
if(request.method.toUpperCase() == 'POST' && request.url == '/mangleCode'){
request.on('data', function(chunk){
body += chunk;
}).on('end', function(){
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
const bodyCode = queryStr.parse(body);
const uglifyCode = uglifyJS.minify(bodyCode.code,{
compress:{
join_vars:false
},
output:{
comments:'all'
}
});
response.write(JSON.stringify({"code":uglifyCode.code}));
response.end()
});
}else{
response.statusCode = 404;
response.end();
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
- node启动这个服务,之后就可以使用任意的程序去调用了:
node server.js
网友评论