From b75b05f8f5995729f7e6cd52380656efd9dfe862 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Mon, 27 Sep 2021 12:12:53 -0400 Subject: [PATCH] feat: intial commit --- README.md | 2 ++ dload-file-inbuild-packages/README.md | 29 +++++++++++++++++ dload-file-inbuild-packages/index.js | 41 ++++++++++++++++++++++++ dload-file-inbuild-packages/package.json | 20 ++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 README.md create mode 100644 dload-file-inbuild-packages/README.md create mode 100644 dload-file-inbuild-packages/index.js create mode 100644 dload-file-inbuild-packages/package.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..444f5b5 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Overview +This is the sandbox for projects using node.js. diff --git a/dload-file-inbuild-packages/README.md b/dload-file-inbuild-packages/README.md new file mode 100644 index 0000000..d0ff44b --- /dev/null +++ b/dload-file-inbuild-packages/README.md @@ -0,0 +1,29 @@ +/* + * SPDX-FileCopyrightText: 2021 Stefan Begerad + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +## Overview +Download file using node.js inbuild packges. + +## Preparation +Run the following command in your favorite terminal to install dependenies. +``` +npm i +``` +## Development setup +Run the following command in your favorite terminal if you fancy log messages for debugging. +``` +export DEBUG=$DEBUG,dload +``` +Run the following command in your favorite terminal to start the service in development mode. +``` +npm run dev +``` +## Production deployment +Run the following command in your favorite terminal to start the service for production mode. +``` +npm run start +``` +## Links +[source](https://www.geeksforgeeks.org/how-to-download-a-file-using-node-js/) \ No newline at end of file diff --git a/dload-file-inbuild-packages/index.js b/dload-file-inbuild-packages/index.js new file mode 100644 index 0000000..ca2e02b --- /dev/null +++ b/dload-file-inbuild-packages/index.js @@ -0,0 +1,41 @@ +/** + * enable reading of environment varialble + */ +require('dotenv').config(); + +/* + * enable debugging + */ +const debug=require('debug')('dload'); + +/* + * set URL constant + */ +const URL=process.env.URL||'to be configured using environment variable .env'; +debug('URL: '+URL) + +/* + * enable file system access + */ +const fs = require('fs'); + +/* + * enable HTTP requests + */ +const http = require('http'); + +/* + * send HTTP GET request + */ +http.get(URL,(res) => { + /* + * image will be stored at this place + */ + const path = `${__dirname}/dload`; + const filePath = fs.createWriteStream(path); + res.pipe(filePath); + filePath.on('finish',() => { + filePath.close(); + console.log('Download Completed'); + }) +}) diff --git a/dload-file-inbuild-packages/package.json b/dload-file-inbuild-packages/package.json new file mode 100644 index 0000000..7d85ed8 --- /dev/null +++ b/dload-file-inbuild-packages/package.json @@ -0,0 +1,20 @@ +{ + "name": "dload-file-inbuild-packages", + "version": "0.0.1", + "description": "", + "main": "index.js", + "scripts": { + "dev": "nodemon index.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Stefan Begerad", + "license": "GPL-3.0", + "devDependencies": { + "nodemon": "^2.0.7" + }, + "dependencies": { + "dotenv": "^8.2.0", + "fs": "0.0.1-security", + "http": "0.0.1-security" + } +}