-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #414 from mohamed-bahaa21/master
added: stream-multiple example
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const express = require("express"); | ||
const bodyParser = require("body-parser"); | ||
const app = express(); | ||
const { stream_multiple } = require('./stream-multiple') | ||
const env = require('./env') | ||
|
||
app.use( | ||
bodyParser.urlencoded({ extended: false }), | ||
bodyParser.json(), | ||
express.static(__dirname + '/public'), | ||
); | ||
|
||
app.get('/', (req, res) => res.send(` | ||
<h1>Thanks Tomás Pollak</h1> | ||
<a href="/stream_files">Stream Multiple Files</a> | ||
`)); | ||
|
||
app.get('/stream_multiple_files', (req, res) => stream_multiple(req, res, env._urls, env.stream_dir)); | ||
|
||
let PORT = process.env.PORT || 3000; | ||
app.listen(PORT, console.log(`Main Server: ${PORT}`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
_urls: [ | ||
"https://images.unsplash.com/photo-1619410283995-43d9134e7656?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80", | ||
"https://images.unsplash.com/photo-1555949963-aa79dcee981c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80", | ||
"https://images.unsplash.com/photo-1511376777868-611b54f68947?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80" | ||
], | ||
_url: "https://images.unsplash.com/photo-1511376777868-611b54f68947?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80", | ||
stream_dir: `public/` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "express-needle", | ||
"version": "1.0.0", | ||
"description": "Express & Needle are friends <3", | ||
"main": "app.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "nodemon app.js" | ||
}, | ||
"keywords": [], | ||
"author": "mohamed-bahaa21", | ||
"license": "ISC", | ||
"dependencies": { | ||
"body-parser": "^1.20.1", | ||
"express": "^4.18.2", | ||
"fs-extra": "^10.1.0", | ||
"needle": "^3.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var needle = require('needle'); | ||
const fs = require('fs-extra') | ||
|
||
function stream_multiple(req, res, _urls, stream_dir, index = 0) { | ||
if (index == 0) { | ||
// initial state | ||
} | ||
|
||
let writeStream; | ||
const uri = _urls[index]; | ||
|
||
if (index == undefined) { | ||
index = 0; | ||
stream_multiple(req, res, _urls, stream_dir, index); | ||
} else { | ||
|
||
writeStream = fs.createWriteStream(`${stream_dir}` + `${index}.jpeg`); | ||
|
||
writeStream.on("ready", () => console.log({ msg: `STREAM::WRITE::READY::${index}` })); | ||
writeStream.on("open", () => console.log({ msg: `STREAM::WRITE::OPEN::${index}` })); | ||
writeStream.on("finish", () => console.log({ msg: `STREAM::WRITE::DONE::${index}` })); | ||
|
||
writeStream.on('close', () => { | ||
if (index >= _urls.length - 1) { | ||
res.redirect('/'); | ||
} else { | ||
stream_multiple(req, res, _urls, stream_dir, index + 1); | ||
} | ||
}) | ||
|
||
needle | ||
.get(uri, function (error, response) { | ||
if (response.bytes >= 1) { | ||
// you want to kill our servers | ||
} | ||
|
||
if (!error && response.statusCode == 200) { | ||
// good | ||
} else { | ||
// then we can retry later | ||
} | ||
}) | ||
.pipe(writeStream) | ||
.on('done', function () { | ||
// needle | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { stream_multiple } |