@@ -2,9 +2,12 @@ var http = require("http"),
2
2
url = require ( "url" ) ,
3
3
path = require ( "path" ) ,
4
4
fs = require ( "fs" ) ,
5
+ config = require ( './config' ) ;
5
6
ports = new Array ( 8123 , 8124 ) ,
6
7
servers = new Array ( ports . length ) ;
7
8
9
+ var res_save = new Array ( ) ;
10
+
8
11
types = {
9
12
".css" : "text/css" ,
10
13
".gif" : "image/gif" ,
@@ -39,11 +42,25 @@ function request_listener(req, res) {
39
42
40
43
path . exists ( pathname , function ( exists ) {
41
44
if ( exists ) {
42
- var content_type = types [ path . extname ( pathname ) ] || "text/plain" ;
43
- res . writeHead ( 200 , { "Content-Type" : content_type } ) ;
45
+ var ext = path . extname ( pathname ) ;
46
+ var content_type = types [ ext ] || "text/plain" ;
47
+
48
+ res . setHeader ( "Content-Type" , content_type ) ;
44
49
45
- fs . readFile ( pathname , function ( err , data ) {
46
- res . end ( data ) ;
50
+ fs . stat ( pathname , function ( err , stat ) {
51
+ var last_modified = stat . mtime . toUTCString ( ) ;
52
+ var if_modified_since = "If-Modified-Since" . toLowerCase ( ) ;
53
+ res . setHeader ( "Last-Modified" , last_modified ) ;
54
+
55
+ if ( req . headers [ if_modified_since ] && last_modified == req . headers [ if_modified_since ] ) {
56
+ res_save . push ( { "status" : 304 , 'pathname' :req . url . slice ( 1 ) } ) ;
57
+ res . writeHead ( 304 , "Not Modified" ) ;
58
+ res . end ( ) ;
59
+ } else {
60
+ fs . readFile ( pathname , function ( err , data ) {
61
+ res . end ( data ) ;
62
+ } ) ;
63
+ }
47
64
} ) ;
48
65
} else {
49
66
res . writeHead ( 404 , { "Content-Type" : "text/html" } ) ;
@@ -65,3 +82,5 @@ for (var i = 0; i < ports.length; i++) {
65
82
servers [ i ] . on ( 'error' , error_handler ) ;
66
83
console . log ( "Server running at http://127.0.0.1:" + ports [ i ] ) ;
67
84
}
85
+
86
+ exports . res_save = res_save ;
0 commit comments