diff --git a/examples/stream-multiple/app.js b/examples/stream-multiple/app.js new file mode 100644 index 000000000..91f5a590b --- /dev/null +++ b/examples/stream-multiple/app.js @@ -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(` +

Thanks Tomás Pollak

+ Stream Multiple Files +`)); + +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}`)); \ No newline at end of file diff --git a/examples/stream-multiple/env.js b/examples/stream-multiple/env.js new file mode 100644 index 000000000..f235eaf97 --- /dev/null +++ b/examples/stream-multiple/env.js @@ -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/` +} \ No newline at end of file diff --git a/examples/stream-multiple/package.json b/examples/stream-multiple/package.json new file mode 100644 index 000000000..adc5da3d3 --- /dev/null +++ b/examples/stream-multiple/package.json @@ -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" + } +} diff --git a/examples/stream-multiple/stream-multiple.js b/examples/stream-multiple/stream-multiple.js new file mode 100644 index 000000000..0012d8485 --- /dev/null +++ b/examples/stream-multiple/stream-multiple.js @@ -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 } \ No newline at end of file