FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NodeServer/native-cluster/server.js at master · HowProgrammingWorks/NodeServer · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
HowProgrammingWorks
/
NodeServer
Public
Notifications
You must be signed in to change notification settings
Fork
44
Star
95
Code
Issues
0
Pull requests
6
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
NodeServer
/
native-cluster
/
server.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (38 loc) · 1.07 KB
Breadcrumbs
NodeServer
/
native-cluster
/
server.js
Copy path
File metadata and controls
44 lines (38 loc) · 1.07 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
'use strict'
;
const
http
=
require
(
'node:http'
)
;
const
cluster
=
require
(
'node:cluster'
)
;
const
os
=
require
(
'node:os'
)
;
const
PORT
=
2000
;
const
user
=
{
name
:
'jura'
,
age
:
22
}
;
const
pid
=
process
.
pid
;
const
routing
=
{
'/'
:
'welcome to homepage'
,
'/user'
:
user
,
'/user/name'
:
(
)
=>
user
.
name
,
'/user/age'
:
(
)
=>
user
.
age
,
}
;
const
types
=
{
object
:
(
o
)
=>
JSON
.
stringify
(
o
)
,
string
:
(
s
)
=>
s
,
number
:
(
n
)
=>
n
.
toString
(
)
,
undefined
:
(
)
=>
'not found'
,
function
:
(
fn
,
par
,
client
)
=>
JSON
.
stringify
(
fn
(
client
,
par
)
)
,
}
;
if
(
cluster
.
isPrimary
)
{
const
count
=
os
.
cpus
(
)
.
length
;
console
.
log
(
`Master pid:
${
pid
}
`
)
;
console
.
log
(
`Starting
${
count
}
forks`
)
;
for
(
let
i
=
0
;
i
<
count
;
i
++
)
cluster
.
fork
(
)
;
}
else
{
const
id
=
cluster
.
worker
.
id
;
console
.
log
(
`Worker:
${
id
}
, pid:
${
pid
}
, port:
${
PORT
}
`
)
;
http
.
createServer
(
(
req
,
res
)
=>
{
const
data
=
routing
[
req
.
url
]
;
const
type
=
typeof
data
;
const
serializer
=
types
[
type
]
;
res
.
setHeader
(
'Process-Id'
,
pid
)
;
res
.
end
(
serializer
(
data
,
req
,
res
)
)
;
}
)
.
listen
(
PORT
)
;
}
Back
|
FazBrowse Home
|
New Git URL