-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (42 loc) · 1.34 KB
/
index.js
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
const http = require('http');
const url = require('url')
const os = require('os');
const cluster = require('cluster')
var StringDecoder = require('string_decoder').StringDecoder
var decoder = new StringDecoder('utf-8')
const startServer = function () {
const server = http.createServer(function (req, res) {
var parsedUrl = url.parse(req.url, true);
res.setHeader('Content-Type', 'application/json')
if (parsedUrl.pathname === '/hello') {
res.writeHead(400)
req.on('data', function (data) {
var yourMessage = '';
yourMessage = decoder.write(data);
res.end(JSON.stringify({ greeting: "Hello!", message: yourMessage }))
})
req.on('end', function () {
res.end(JSON.stringify({ greeting: "Hello!" }))
})
} else {
res.writeHead(200)
res.end(JSON.stringify({ youUsed: 'other route' }))
}
})
server.listen(3000, function () {
console.log('server is listening on port 3000')
});
}
const init = function (callback) {
if (cluster.isMaster) {
for (var i = 0; i < os.cpus().length; i++) {
cluster.fork()
}
}else{
startServer()
}
callback();
}
if(require.main===module){
init(function(){});
}