Tracking tool for monitoring progress of HTTP POST requests

Hi, I am still a beginner when it comes to NodeJS and its modules. I could really use some assistance with creating a progress bar for an application I'm working on. Right now, the progress bar only reaches 100% upon completion and I suspect my piping technique might be incorrect. Any guidance or help would be greatly appreciated!

In addition, I am also sending data through a socket if that detail makes a difference.

const request = require('request');
const io = require('socket.io')(http)
const fs = require('fs');
var progress = require('progress-stream');

var str = progress({
    time: 1000
});

str.on('progress', function(progress) {
    console.log(Math.round(progress.percentage) + '%');
});


io.on('connection', (socket)=>{
socket.on('fileUploader',()=>{
    let formData = {
            preview_file: fs.createReadStream(filePath};
        request.post({
                url: 'http://httpbin.org/post',
                formData: formData
            }, 
                function optionalCallback(err, httpResponse, body) {
                if (err) {
                    return console.error('upload failed:', err);
                }
                console.log('Upload successful!  Server responded with:', body);
        }).pipe(str);
    })
})

Answer №1

If you're looking to make HTTP requests, consider trying out axios. axios offers an onUploadProgress method, allowing you to utilize the "progressEvent" callback for obtaining upload percentage.

For more detailed information regarding axios usage, refer to their documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing the functionality of hidden input fields using jQuery

I'm looking to dynamically update the value of a hidden input element when a button is clicked. Here's what I have so far: Hidden Input Element: <input type="hidden" value="action" id="action" /> Buttons: <INPUT name=submit value=~#S ...

Tips for updating content (wishlist) without the need to refresh the page

Currently experimenting with the TMDb API to enhance my PHP skills. I've successfully created a wishlist feature and now looking to optimize the script. A function is implemented on each movie page for adding movies to the wishlist. function getButt ...

Tips for transforming your angular application into a custom npm package or library

Recently, I've been working on an Angular application that fetches photos and videos from a server and displays them as a slide one at a time. Now, I'm faced with the task of integrating this functionality into another app. Is there a method to t ...

Querying Nested Arrays in MongoDB using Node.js

In my MongoDB database, I have a collection structured like this: [ { "machine": 1, "status": true, "comments": [ { "machine": 1, "status": false, "emp" ...

I am interested in utilizing node.js to input data into a mongodb Database

Currently, I am working on writing data to a JSON file and retrieving it back to an HTML page for display. Now, I want to achieve the same functionality with a MongoDB database. I have made some attempts, but unfortunately, it is not functioning as expecte ...

Can React Router be integrated with Material UI tabs?

I have made some changes to the code of the Tabs component in material ui, <AppBar position="static"> <Tabs variant="fullWidth" value={value} onChange={handleChange} aria-label="nav tabs example" > < ...

"Implementing x2js in your project on-the-fly with the help

Hey there, does anyone have the link for x2js that they could share with me? I'm interested in loading this JavaScript file dynamically. I tried using the code below but it didn't work: EffectaJQ.ajax({ async: false, url: "https ...

ajax used to retrieve xml data and process it

I've been working on retrieving information from an API (specifically BBB) and the response I'm getting looks like this: <?xml version="1.0"?> <response> <returncode>SUCCESS</returncode> <meetingID>ldapreade ...

What is the best way to find the most commonly used category for a product in a MongoDB collection?

I've been diving into MongoDB and encountered a challenge with my current task. I'm trying to figure out how to determine the most frequently used category in a collection. According to this JSON, the most used category is CIES. My goal is to dis ...

Utilizing X-editable and Parsley to trigger dual Ajax calls

Here is a snippet of code I am working with: $.fn.editable.defaults.mode = 'inline'; var editable = $('.x-editable').editable({ send: 'always', validate: function() { var form = editable.next().find('form ...

Intercept response prior to being delivered to the client

I have the following code snippet which can be utilized as middleware: module.exports=function(callback) { callbacks.push(callback); return function(req,res,next) { if (!res.hijacked) { res.hijacked=true; } else { ...

When navigating using links in NodeJS+Express, I am unable to load the CSS files

I am a beginner with Node.js. Below is the code I used to make the server run in index.js: var express = require('express'), views = require('./routes/views'); var app = express(); // Get an instance of router //var router = ex ...

How long does jQuery animation last until completion?

Within my project, I am utilizing the animationComplete function from the jQuery mobile library. You can find this function at The animation in my project involves a sequence of objects with various tasks to be executed at each point of the animation. The ...

The attempt to add a note with a POST request to the /api/notes/addnote endpoint resulted in a

I'm facing an issue while trying to send a POST request to the /api/notes/addnote endpoint. The server is returning a 404 Not Found error. I have checked the backend code and made sure that the endpoint is correctly defined. Here are the specifics of ...

Guide on implementing jQuery Validation plugin with server-side validation at the form level

Does anyone have a suggestion for how to handle server-side validation errors that occur after passing the initial client-side validation on a form? $("#contact_form").validate({ submitHandler: function(form) { $.ajax({ type: 'POST', ...

How can I securely store passwords for web scraping with Puppeteer to ensure maximum safety?

Looking for advice on scraping a website that requires login. The current code saves username and password in a config JSON file, which poses a security risk if the file is accessed by unauthorized individuals. Is there a more secure method, such as encr ...

Inadequate data being sent to the server from Angular2 post request

Currently, I have a form field whose value I am passing to a service as this.form.value. However, when I log this.form.value on the console, I see Object { email: "zxzx", password: "zxzxx" }. Despite this, when I send the same data to the service and make ...

Error occurs when using Express.js in combination with linting

https://www.youtube.com/watch?v=Fa4cRMaTDUI I am currently following a tutorial and attempting to replicate everything the author is doing. At 19:00 into the video, he sets up a project using vue.js and express.js. He begins by creating a folder named &apo ...

Error message received from GitHub when attempting to create a repository through the REST API states that JSON cannot

I am currently in the process of learning how to use REST APIs for GitHub, and my current project involves creating a new repository using JavaScript. Below is the function I have written for this purpose, which includes generating a token and granting all ...

Transform Objects Array from AJAX Response into a Distinct JSON Entity

I am encountering a problem with a sample endpoint that is returning [object Object] for JSON data, and I can't figure out why. Mock API Initially, my code was a bit confusing, but fortunately, I found a clearer solution in another answer. functio ...