Skip to content

Commit d5498c9

Browse files
committed
Testing with stream methods
1 parent 2467350 commit d5498c9

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

server.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Node simpe http server
2-
32
// Get optional args
43
var args = process.argv.splice(2);
54
var formattedArgs = {};
@@ -20,10 +19,13 @@ if (formattedArgs['--root']) {
2019

2120
var PORT = formattedArgs['--port'] || 3000;
2221

22+
23+
2324
// Required modules
2425
var http = require('http');
2526
var fs = require('fs');
2627
var url = require('url');
28+
var ROOT_PATH = process.cwd() + '/';
2729

2830
var types = {
2931
'js' : 'application/javascript',
@@ -55,6 +57,7 @@ function getFile (url) {
5557
function notFound(url, res) {
5658
console.log('Could find ', url);
5759
res.writeHead(404, {"Content-Type": 'text/html'});
60+
res.end();
5861
}
5962

6063
var server = http.createServer();
@@ -68,15 +71,9 @@ server.on('request', function(req, res) {
6871
type = getType(path);
6972
file = getFile(path);
7073

71-
fs.readFile(file, function (err, data) {
72-
if (err) {
74+
fs.createReadStream(file).on('error', function () {
7375
notFound(path, res);
74-
} else {
75-
res.writeHead(200, {"Content-Type": type});
76-
res.write(data);
77-
}
78-
res.end();
79-
});
76+
}).pipe(res)
8077
});
8178

8279
server.listen(PORT, function () {

0 commit comments

Comments
 (0)