forked from ningxiao/NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (70 loc) · 1.55 KB
/
app.js
File metadata and controls
71 lines (70 loc) · 1.55 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
"use strict";
/**
*
* 2015.10.31 07:53
* 实验使用node5创建http请求
*
**/
const fs = require('fs');
const os = require('os');
const url = require('url');
const http = require('http');
const zlib = require("zlib");
const path = require('path');
const cluster = require('cluster');
const islog = true;
let httpserver, port = 80,
reqs = 0,
pids = {};
function getip() {
let list, hostname = os.hostname(),
network = os.networkInterfaces();
for (let key in network) {
list = network[key];
for (let i = 0, len = list.length; i < len; i++) {
if (list[i].family == "IPv4") {
return list[i].address;
}
}
}
return "127.0.0.1";
};
httpserver = http.createServer(function(request, response) {
response.write('callback({"status": 200,"data":""})');
response.end();
})
if (cluster.isMaster) {
let cpus = os.cpus().length;
for (let i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('[master] ' + 'exit worker' + worker.id + ' died');
});
} else {
function main(argv) {
if (argv.length == 1) {
port = argv[0];
}
httpserver.on('error', function(error) {
if (error.code == 'EADDRINUSE') {
console.log('服务端口被占用');
}
});
console.log(getip(), '--', process.pid);
httpserver.listen(port);
}
/**
* 退出输出日志
**/
process.on('exit', function(error) {
console.log("服务器退出");
});
/**
* 监听异常退出输出日志
**/
process.on('uncaughtException', function(error) {
console.log(error.toString());
});
main(process.argv.slice(2));
}