forked from Elpugna/Node-Express-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
22 lines (20 loc) · 695 Bytes
/
app.js
File metadata and controls
22 lines (20 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//2235 kB = 2.235Mb
var http = require('http');
var fs = require('fs');
http
.createServer(function(req, res){
//If we use it in this way, the big.txt will be sent as a solid packet and we prefer it in separate chunks of data.
// const text = fs.readFileSync('./content/big.txt', 'utf8');
// res.end(text);
const fileStream = fs.createReadStream('./content/big.txt', 'utf8');
//
fileStream.on("open", ()=>{
//pipe method pushes from the readStream into the correct stream
//let us have the Content-Length: "chunked" in the response headers
fileStream.pipe(res)
})
fileStream.on("error",(err)=>{
res.end(err)
})
})
.listen(5000)