Why stdout.write() is not working in Terminal?

Viewed 37

I am novice in Node JS.

I am trying to test cURL request on my localhost:3000 port. I am using Sublime Text 4 text editor. cURL request on Windows Terminal works fine but Loading Bar is not showing up in terminal. Instead it is showing up in Sublime Text Console.

Sublime Text Console

Here's Terminal Output without loading bar

Windows Terminal Output

I've tried the following code :

const chalk = require("chalk");
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const express = require("express");
const app = express();

const processes = require("process");
const rdl = require("readline");

class LoadingBar {
    constructor(size) {
        this.size = size;
        this.cursor = 0;
        this.timer = null;
    }
    start() {
        processes.stdout.write("\x1B[?25l");
        processes.stdout.write("[");
        for (let i = 0; i < this.size; i++) {
            processes.stdout.write("-");
        }
        processes.stdout.write("]");
        this.cursor = 1;
        rdl.cursorTo(processes.stdout, this.cursor, 0);
        this.timer = setInterval(() => {
            processes.stdout.write("#");
            this.cursor++;
            if (this.cursor >= this.size) {
                clearTimeout(this.timer);
                processes.stdout.write("\x1B[?25h");
            }
        }, 100);
    }
}

const loader = new LoadingBar(25);

const obj = {
    "Response": 200,
    "Operation": 'successful',
};

app.get("/response", (req, res) => {
    loader.start();
    res.json(obj);
});

app.listen(3000, () => {
 console.log("Server running on port 3000");
});

I want to make cURL request from Terminal and after that Loading Bar will show up and then I will get the obj. But for some reasons stdout.write() is not working in Terminal. What should I do?

0 Answers
Related