-
Notifications
You must be signed in to change notification settings - Fork 2
/
express-stream.js
38 lines (28 loc) · 799 Bytes
/
express-stream.js
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
35
36
37
38
'use strict'
const express = require('express')
const fs = require('fs')
const path = require('path')
const port = 3000
const app = express()
const responseTime = require('response-time')
app.use(responseTime())
const largeImagePath = path.join(__dirname,'img/output.jpg')
app.get('/non-stream', (req, res) => {
fs.readFile(largeImagePath, (error, data) => res.end(data))
})
app.get('/non-stream2', (req, res) => {
let file = fs.readFileSync(largeImagePath)
res.end(file)
})
app.get('/stream', (req, res) => {
let stream = fs.createReadStream(largeImagePath)
stream.pipe(res)
})
app.get('/stream2', (req, res) => {
var stream = fs.createReadStream(largeImagePath)
stream.on('data', (data) => {
res.write(data)
})
stream.on('end', () => res.end())
})
app.listen(port)