@@ -6,21 +6,21 @@ api.http = require('http');
6
6
api . fs = require ( 'fs' ) ;
7
7
8
8
// Cache
9
- let cache = { } ;
9
+ const cache = { } ;
10
10
11
11
// HTTP Server
12
12
api . http . createServer ( ( req , res ) => {
13
13
14
14
// Parse cookies
15
- let cookie = req . headers . cookie ,
16
- cookies = { } ;
17
- if ( cookie ) cookie . split ( ';' ) . forEach ( ( item ) => {
18
- let parts = item . split ( '=' ) ;
15
+ const cookie = req . headers . cookie ;
16
+ const cookies = { } ;
17
+ if ( cookie ) cookie . split ( ';' ) . forEach ( item => {
18
+ const parts = item . split ( '=' ) ;
19
19
cookies [ ( parts [ 0 ] ) . trim ( ) ] = ( parts [ 1 ] || '' ) . trim ( ) ;
20
20
} ) ;
21
21
22
22
// Logging
23
- let date = new Date ( ) . toISOString ( ) ;
23
+ const date = new Date ( ) . toISOString ( ) ;
24
24
console . log ( [ date , req . method , req . url ] . join ( ' ' ) ) ;
25
25
26
26
// Serve from cache
@@ -36,7 +36,7 @@ api.http.createServer((req, res) => {
36
36
'Set-Cookie' : 'mycookie=test' ,
37
37
'Content-Type' : 'text/html'
38
38
} ) ;
39
- let ip = req . connection . remoteAddress ;
39
+ const ip = req . connection . remoteAddress ;
40
40
res . write ( '<h1>Welcome</h1>Your IP: ' + ip ) ;
41
41
res . end ( '<pre>' + JSON . stringify ( cookies ) + '</pre>' ) ;
42
42
}
@@ -46,12 +46,12 @@ api.http.createServer((req, res) => {
46
46
// Some business logic
47
47
api . fs . readFile ( './person.json' , ( err , data ) => {
48
48
if ( ! err ) {
49
- let obj = JSON . parse ( data ) ;
49
+ const obj = JSON . parse ( data ) ;
50
50
obj . birth = new Date ( obj . birth ) ;
51
- let difference = new Date ( ) - obj . birth ;
51
+ const difference = new Date ( ) - obj . birth ;
52
52
obj . age = Math . floor ( difference / 31536000000 ) ;
53
53
delete obj . birth ;
54
- let sobj = JSON . stringify ( obj ) ;
54
+ const sobj = JSON . stringify ( obj ) ;
55
55
cache [ req . url ] = sobj ;
56
56
57
57
// HTTP reply
@@ -66,12 +66,12 @@ api.http.createServer((req, res) => {
66
66
} else if ( req . method === 'POST' ) {
67
67
68
68
// Receiving POST data
69
- let body = [ ] ;
69
+ const body = [ ] ;
70
70
req . on ( 'data' , ( chunk ) => {
71
71
body . push ( chunk ) ;
72
72
} ) . on ( 'end' , ( ) => {
73
73
let data = Buffer . concat ( body ) . toString ( ) ;
74
- let obj = JSON . parse ( data ) ;
74
+ const obj = JSON . parse ( data ) ;
75
75
if ( obj . name ) obj . name = obj . name . trim ( ) ;
76
76
data = JSON . stringify ( obj ) ;
77
77
cache [ req . url ] = data ;
0 commit comments