-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstep_by_step.js
More file actions
34 lines (33 loc) · 989 Bytes
/
step_by_step.js
File metadata and controls
34 lines (33 loc) · 989 Bytes
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
var http = require('http'),
fs = require('fs'),
sys = require('sys'),
step = require('step'),
file_path = __dirname + '/web.gif',
file_size;
step(
function get_file_size() {
fs.stat(file_path, this);
// 'this' passes the result to the next function
},
function store_file_size(err, stat) {
file_size = stat.size;
this();
// this() will call the next function
},
function read_file_into_memory() {
fs.readFile(file_path, this);
},
function create_server(err, file_content) {
if(err) {
throw err;
};
http.createServer(function(request,response) {
response.writeHead(200, {
'Content-Type' : 'image/gif',
'Content-Length' : file_size
});
response.end(file_content);
}).listen(4000);
console.log('Listening on port 4000.');
}
);