word.js
3.1 KB
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
/**
* 代码来源:http://www.thinkphp.cn/topic/27567.html
* 如需编译请使用 pkg -t node12-win word.js 命令编译成word.exe文件
*/
var service = {
http: require('http'),
url: require('url'),
querystring: require('querystring'),
fs: require('fs'),
config: {
timeout: 60000,
charset: 'utf8',
port: 10101,
host: '127.0.0.1'
},
router: {
index: function (res, query) {
res.end('Server is running!');
},
check: function (res, query) {
var result = {status: 1, info: 'success'};
result = JSON.stringify(result);
if (typeof query.callback == 'string') {
result = query.callback + '(' + result + ')';
}
res.end(result);
},
word: function (res, query) {
var _this = service;
var result = {status: 0, info: 'error'};
if (typeof query.file == 'string') {
var img = query.file.match(/file:\/\/+(localhost)?(\S+\.(png|jpg|jpeg|gif|bmp))/i);
console.log(img);
if (img) {
var base64 = _this.base64_encode(img[2]);
result.status = 1;
result.index = query.index;
result.info = 'data:image/' + img[3] + ';base64,' + base64;
}
}
result = JSON.stringify(result);
if (typeof query.callback == 'string') {
result = query.callback + '(' + result + ')';
}
res.end(result);
}
},
start: function () {
var _this = this;
var Server = _this.http.createServer(function (req, res) {
var URL = _this.url.parse(req.url);
var receive = [];
var router = null;
switch (URL.pathname) {
case '/word':
router = _this.router.word;
break;
case '/check':
router = _this.router.check;
break;
default:
router = _this.router.index;
}
req.setEncoding(_this.config.charset);
req.addListener('data', function (data) {
receive.push(data);
});
res.writeHead(200, {'Content-Type': 'text/plain'});
res.on("close", function () {
console.log("res closed");
});
req.on("close", function () {
console.log("req closed");
});
req.addListener('end', function () {
router(res, _this.querystring.parse(URL.query));
});
});
Server.listen(_this.config.port, _this.config.host, 1024);
Server.setTimeout(_this.config.timeout, function (cli) {
cli.end('timeout\n');
});
console.log('Server running at http://' + _this.config.host + ':' + _this.config.port);
},
//base64
base64_encode: function (file) {
var bitmap = this.fs.readFileSync(file);
return new Buffer(bitmap).toString('base64');
}
};
service.start();