-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
91 lines (91 loc) · 2.89 KB
/
Copy pathapp.js
File metadata and controls
91 lines (91 loc) · 2.89 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
require('shelljs/global');
~function a() {
setTimeout(function () {
exec('screencapture -C -r -t jpg -x -m -o ./IMG.jpg');
a();
}, 500);
}();
//引入http模块
var http = require("http");
var url = require('url');
var fs = require('fs');
var path = require('path');
var os = require('os');
//设置主机名
var hostName = function getLocalIp() { //获取内网ip
var map = [];
var ifaces = os.networkInterfaces();
for (var dev in ifaces) {
if (ifaces[dev][1].address.indexOf('192.168') != -1) {
return ifaces[dev][1].address;
}
}
return map;
}();
//设置端口
var port = 8088;
//创建服务
var server = http.createServer(function (request, response) {
//console.log(request.url);
var pathname;
if (request.url == '/') {
pathname = 'index.html';
} else {
pathname = url.parse(request.url).pathname;
}
var realPath = path.join("./", pathname);
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
fs.exists(realPath, function (exists) {
if (!exists) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write("This request URL " + pathname + " was not found on this server.");
response.end();
} else {
fs.readFile(realPath, "binary", function (err, file) {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.end(err);
} else {
var contentType = {
"css": "text/css",
"gif": "image/gif",
"html": "text/html",
"ico": "image/x-icon",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"pdf": "application/pdf",
"png": "image/png",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tiff": "image/tiff",
"txt": "text/plain",
"wav": "audio/x-wav",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"xml": "text/xml"
}[ext] || "text/plain";
response.writeHead(200, {
'Content-Type': contentType
});
response.write(file, "binary");
response.end();
}
});
}
});
});
server.listen(port, hostName, function () {
console.log(`服务器运行在 http://${hostName}:${port}`);
});