Process HTTP request in batch in NodeJs

Problem

I have a single server that can process 10 HTTP requests in a minute. If I get more than 10 requests, then I would like to save them somewhere to respond to them later.

Latency is not a problem here, clients can wait

What I did

import express from "express";
const app = express();
const Q = [];

async function scheduler() {
  // if Q is empty then wait for  request to come in
  if (!Q.length) {
    setTimeout(scheduler, 1000);
    return;
  }

  // send response to all currently available requests
  let currentLength = Q.length;
  for (let i = 0; i < currentLength; i++) Q[i].res.send("Q" + Q[i].id);

  // remove the requests whose response is sent
  Q.slice(0, currentLength);
  scheduler();
}

scheduler();

app.get("/", (req, res) => {
  // store HTTP requests
  Q.push({ req, res, id: req.query.id });
});

app.listen(process.env.PORT || 5000, () => console.log("Server is running..."));

I tried to store the requests in the array, then send a response by another function.

This is just a simulation

I tried the following script to test the script

import axios from "axios";

const promises = [];
for (let i = 0; i < 25; i++) {
  promises.push(axios.get(`http://localhost:5000?id=${i}`));
}

console.time();
Promise.allSettled(promises).then((res) => {
  console.timeEnd();
  console.log(res.map((httpRes) => httpRes?.value?.data));
});

I am getting successful response for each request

[
  'Q0',  'Q1',  'Q2',  'Q3',
  'Q4',  'Q5',  'Q6',  'Q7',
  'Q8',  'Q9',  'Q10', 'Q11',
  'Q12', 'Q13', 'Q14', 'Q15',
  'Q16', 'Q17', 'Q18', 'Q19',
  'Q20', 'Q21', 'Q22', 'Q23',
  'Q24'
]

But, still I am getting the below error on server terminal

(node:26071) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (/home/rahul/Desktop/projects/temp/puppeteer/node_modules/express/lib/response.js:776:10)
    at ServerResponse.send (/home/rahul/Desktop/projects/temp/puppeteer/node_modules/express/lib/response.js:170:12)
    at scheduler (file:///home/rahul/Desktop/projects/temp/puppeteer/index.js:43:52)
    at Timeout.scheduler [as _onTimeout] (file:///home/rahul/Desktop/projects/temp/puppeteer/index.js:47:3)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:26071) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:26071) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

React form input values in JS

I am using NextJS with bulma CSS to create a simple application. I have this following form:

const MyPage = () => {
const [firstName, setFirstName] = useState('')
const [secondName, setSecondName] = useState('')

const createNftHandler = async() => {
   // Todo: perform some action with firstName and secondName
}

const updateFirstName = event => {
    setFirstName(event.target.value)
}

const updateSecondName = event => {
    setSecondName(event.target.value)
}

return (
<section className='mt-5'>
    <div className='container'>
        <div className='field'>
            <label className='label'>My Form</label>
            <div className='control'>
                <input onChange={updateFirstName} className='input' type='type' placeholder='Enter First Name'></input>
            </div>
        </div>
        <div className='field'>
            <div className='control'>
                <input onChange={updateSecondName} className='input' type='type' placeholder='Enter Second Name'></input>
            </div>
        </div>
        <button onClick={createUser} className='button is-primary'>Create</button>
    </div>
</section>
)
}
export default MyPage

I have to call updateFirstName and updateSecondName on every input change.
I want to get these input field’s value on createUser() function call only. Please suggest how to do it or any other better approach.

How to allow javascript form to skip questions without producing text which assumes an answer?

I have created a page with a form for people to complete, which when completed produces a text file of the person’s answers.

However I want people to have the option of skipping a question if they don’t want to answer it.

The problem with the code below is that if you skip a question it still produces the preceding/following text and spaces.

i.e. if a person doesn’t want to answer the question about their age, the below code will still produce text which reads “Age:[space][space]” in the text file.

If someone skips a question how to I stop the text file still producing the above? Ideally I want it to be completely ignored in the text file if the question hasn’t been answered.

    // Get the data from each element on the form.
    const name = document.getElementById('txtName');
    const age = document.getElementById('txtAge');
    const email = document.getElementById('txtEmail');
    const country = document.getElementById('selCountry');
    const msg = document.getElementById('msg');
    
    // This variable stores all the data.
    let data = 
        'r Name: ' + name.value + ' rn ' + 
        'Age: ' +age.value + ' rn ' + 
        'Email: ' + email.value + ' rn ' + 
        'Country: ' + country.value + ' rn ' + 
        'Message: ' + msg.value;
    
    // Convert the text to BLOB.
    const textToBLOB = new Blob([data], { type: 'text/plain' });
    const sFileName = 'formData.txt';      // The file to save the data.

    let newLink = document.createElement("a");
    newLink.download = sFileName;

    if (window.webkitURL != null) {
        newLink.href = window.webkitURL.createObjectURL(textToBLOB);
    }
    else {
        newLink.href = window.URL.createObjectURL(textToBLOB);
        newLink.style.display = "none";
        document.body.appendChild(newLink);
    }

    newLink.click(); 

Что такое триггерная рассылка? [closed]

ТРИГГЕРНАЯ РАССЫЛКА — это последовательность писем, которые отправляются при наступлении определенных событий. Это может быть брошенная корзина, отсутствие активности пользователя в течение какого-либо времени, успешно оформленный заказ.
★★★★★★★★★★★★★★★★★★★★★★★
Для примера мы создадим рассылку вручную, не используя готовые шаблоны. В основных параметрах рассылки вы можете:

Ввести название рассылки. Будьте внимательны и обязательно продумайте название. Его увидит получатель.
Выберите сайт, от имени которого будет отправлена рассылка.
Добавьте описание для письма. Его тоже увидит получатель.
Выберите условие для запуска рассылки. На этом пункте мы остановимся подробнее ниже.
Выберите условие достижение цели. Оно будет отображаться в метрике.

How to click on multiple dropdown & select same values from all dropdown in cypress

I am able to click on all dropdowns but am not able to select values from them one by one.

cy.xpath('/html/body/app-root/div/mat-sidenav-container/mat-sidenav-content/app-configurable-product-management/form/div[2]/mat-card/mat-card-content/app-configurable-product-inventory/form/div/div/div/div')
            .each($row => {
                //Click on drop down
                cy.wrap($row).find(':nth-child(4) > .common-form-field-width > .mat-form-field-wrapper > .mat-form-field-flex > .mat-form-field-infix').click({timeout: 2000, force:true})
                //Select same values from all dropdown (Not working)
                cy.contains('In Stock').click({timeout: 2000, force:true})
                cy.wait(2000)
            })

Map function stores the value of the last object in the array to all values

I am getting data from Firestore and using the map function to display all of that data. I am sending one of the arguments within a URL to get an image. I am sending a cryptocurrency pair in a URL to get an image in response. I am also displaying the pair in the table. However, when I send the same symbol in the map function, all of my pairs get the same pair sent in the url, the last pair of the data.
To simplify the issue:

Steps:

  1. Map function to display data
  2. Pair attribute is sent in the link to get image with every map function (click button to see image)
  3. Pair function also displayed in the table

Result:

  1. The pair in the table is unique and displaying correctly
  2. All urls in the map function get the same pair, the pair of the last object.

Code snippet

 {data.map((trade) => (
            <tr>
              <td>
                <div style={{ display: "flex" }}>
                  <img
                    src={trade.image}
                    style={{
                      width: "30px",
                      height: "30px",
                      marginTop: "13px",
                      marginRight: "5px",
                    }}
                    alt="btc"
                  />
                  <p className="coin-name">{trade.symbol.baseAsset}</p>
                </div>
              </td>
              <td>{trade.symbol.symbol}</td>
              <td>{trade.qty}</td>

              <td>{Math.round(trade.multiplier)}x avg volume</td>

              <td>{trade.price}</td>
              <td>{trade.exchangeDetails.name.toUpperCase()}</td>
              <td>{Math.round(trade.tradeValue * 1000) / 1000}</td>
              <td>
                <div>
                  <Button
                    onClick={handleOpen}
                    style={{ backgroundColor: "#142F37 ", color: "white" }}
                  >
                    View Graph
                  </Button>
                  <Modal
                    open={open}
                    onClose={handleClose}
                    aria-labelledby="modal-modal-title"
                    aria-describedby="modal-modal-description"
                  >
                    <Box sx={style}>
                      <img
                        src={`https://2zpbbz.chart-img.com/v1/tradingview/advanced-chart?symbol=${trade.symbol.symbol}`}
                        alt="No graph for this pair"
                        style={{ width: "500px" }}
                      />
                    </Box>
                  </Modal>
                </div>
              </td>
            </tr>
          ))}

UI

Get image as array buffer and display

I have written a file to an array buffer and stored on IPFS. At another point I wish to pull this buffer from IPFS and have the image displayed on the page. I am struggling with this as the blob does not seem to be located.

Adding photo as array buffer:

// Read photo
let photoFile = e.target.photo.files[0];
let reader = new window.FileReader();
let that = this;
reader.onloadend = function (e) {
  let res = reader.result;
  that.setState({photoString: res});
}

if (photoFile) {
  await reader.readAsArrayBuffer(photoFile);
}

// Add photo to IPFS
await ipfs.add(photoFile).then(async (result, error) => {
  let photoHash = result.path;
  if (error) {
    console.error(error)
  }
});

When trying to get from IPFS and display:

let holderPhoto = await $.get(`https://ipfs.infura.io/ipfs/${IPFSPhotoHash}`);
    const blob = new Blob( [ holderPhoto ] );
    const url = URL.createObjectURL( blob );
    const img = document.getElementById( 'photo' );
    img.image = require(url);
    img.onload = e => URL.revokeObjectURL( url );


<CardMedia id="photo" component="img" height="420" alt="Licence Photo"/>

Error:
components sync:2 Uncaught (in promise) Error: Cannot find module ‘blob:http://localhost:3000/ff4f9e6a-7fae-4cc2-93b3-a6234719c87f’

How to remove any text inside parentheses in a text node in a dynamically created iframe list called in a Bootstrap modal?

So I have a bootstrap modal that calls a .html file in an iframe (my .html file on the same domain) that always produces a different result which is what I need. However the contents generated in the list always creates a text node inside each li of the list containing text wrapped in parentheses. I want to keep all the text that is not inside the parentheses, but remove ANY text that appears inside parentheses ().

So my modal is called here:

<div class="modal-body"><iframe id="researchiframe" name="display-frame" style="width:100%;height:600px;"></iframe></div>

Then it loads my .html file into the iframe and displays the information I want in a list.

<div class="row">
<div class="col-lg-8 offset-lg-3" id="research_result">
<h4 id="research_result_title">Details</h4>

<center>
<ul id="research_result_details" style="padding:0px">
// text I want to remove appears in here //
</ul>
</center>

The list that it generates comes out like this…

<li><h5 class="title" style="display:inline-block;">My Title</h5>:These are the results I want to keep (Theses are the results I want to remove)</li>

So inside the li it creates text that I want to keep, and also text inside (parentheses) I want to remove.

Any help with this is greatly appreciated.

Shouldn’t await and then behave the same way?

Until today, I thought that await and then were 2 different ways to handle promises results.

I used nodemailer.createTestAccount to do some e2e testing and I stumbled in this counter-intuitive behaviour.

This is what the below code does:

  • I have 2 test functions: testA and testB (they should do the same exact thing)
  • I call the 2 functions and I get some output
  • testA seems to work properly returning different value every call
  • testB seems to return always the same value
const nodemailer = require("nodemailer");

const testA = async () => {
  nodemailer.createTestAccount().then(account => { console.log("testA:", account.user) })
  nodemailer.createTestAccount().then(account => { console.log("testA:", account.user) })
  nodemailer.createTestAccount().then(account => { console.log("testA:", account.user) })
}

const testB = async () => {
  const test1 = await nodemailer.createTestAccount()
  console.log("testB:", test1.user)

  const test2 = await nodemailer.createTestAccount()
  console.log("testB:", test2.user)

  const test3 = await nodemailer.createTestAccount()
  console.log("testB:", test3.user)
}

testA()
testB()

The result (hand sorted for readability):

testA: [email protected]
testA: [email protected]
testA: [email protected]

testB: [email protected]
testB: [email protected]
testB: [email protected]

Can someone explain me why is this happening?

CAPTCHA Error: POST does not match SESSION key

I am following a CAPTCHA tutorial on TutsPlus.com and I keep getting the ‘Incorrect Captcha message, meaning my $_POST user input does not match the stored $_SESSION string.

Upon further probing, I tried to echo $_SESSION[‘captcha_text’] and I got the error: ‘Undefined array key.’ As if I hadn’t stored the solution at all.

I have looked at my logs and used developer tools but the only other error is ‘Uncaught TypeError: Cannot set properties of null (setting ‘onclick’)’ which is probably why my javascript won’t run either.

Here is my captcha.php script:

// Store all characters we wish to use to generate CAPTCHA string. Only capital letters to avoid confusion.
$permitted_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

/**
 * Generate cryptographically secure random numbers using random_int() or non-secure random numbers using mt_rand()
 * @param string permitted chars used in CAPTCHA
 * @param int length of random string, default = 5
 * @param bool secure or not, default is secure
 * @return string random string
 */
function secure_generate_string($input, $strength = 5, $secure = TRUE) {
    $input_length = strlen($input);
    $random_string = '';
    for($i = 0; $i < $strength; $i++) {
        if($secure) {
            $random_character = $input[random_int(0, $input_length - 1)];
        } else {
            $random_character = $input[mt_rand(0, $input_length - 1)];
        }
        $random_string .= $random_character;
    }

    return $random_string;
}

// Render the CAPTCHA background. Image will be 200x50px in size and use five different colors for background.
$image = imagecreatetruecolor(200, 50);

imageantialias($image, true);

$colors = [];

// Generate random colors
$red = rand(125, 175);
$green = rand(125, 175);
$blue = rand(125, 175);

// Generate progressively darker shades of original colors. Store colors in array with darkest color as last element. 
for($i = 0; $i < 5; $i++) {
    $colors[] = imagecolorallocate($image, $red - 20*$i, $green - 20*$i, $blue - 20*$i);
}

// Lightest color used to fill whole background of image.
imagefill($image, 0, 0, $colors[0]);

// Draw rectangles at random locations on original image. Thickness of rectangles varies between 2 and 10, while color chosen randomly from last four values of $colors array
for($i = 0; $i < 10; $i++) {
    imagesetthickness($image, rand(2, 10));
    $rect_color = $colors[rand(1,4)];
    imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $rect_color);
}

// Letters are mix of black and white 
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$textcolors = [$black, $white];

// Fonts downloaded from Google to get variation in characters
$fonts = [FONT . 'Acme' . DS . 'Acme-Regular.ttf', FONT . 'Ubuntu' . DS . 'Ubuntu-Regular.ttf', FONT . 'Merriweather' . DS . 'Merriweather-Regular.ttf', FONT . 'Gideon_Roman' . DS . 'GideonRoman-Regular.ttf', FONT . 'Yeseva_One' . DS . 'YesevaOne-Regular.ttf'];

$string_length = 6;
$captcha_string = secure_generate_string($permitted_chars, $string_length);

$_SESSION['captcha_text'] = $captcha_string;

// Padding 15px on both sides of image. Leftover space (170px) is divided equally among all CAPTCHA letters
for($i = 0; $i < $string_length; $i++) {
    $letter_space = 170/$string_length;
    $initial = 15;

    imagettftext($image, 20, rand(-15, 15), $initial + $i*$letter_space, rand(20, 40), $textcolors[rand(0, 1)], $fonts[array_rand($fonts)], $captcha_string[$i]);
}

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

Here is my captcha.js script:

var refreshButton = document.querySelector('.refresh-captcha');

refreshButton.onclick = function() {
    document.querySelector('.captcha-image').src = 'captcha.php?' + Date.now();
}

My login.php where the captcha is displayed:


        <div class='captcha-container'>
            <label for='captcha'>Please Enter the Captcha Text</label>
            <br>
            <img src='<?php echo FUNCT_URL;?>captcha.php' class='captcha-image' alt='captcha'>
            
            <i class='fa-solid fa-arrow-rotate-right re
            fresh-captcha'></i>
            <br>
            <input class='input-field' type='text' id='captcha' name='captcha_challenge' pattern='[A-Z]{6}' required>
        </div>

        <button class='submit-btn' type='submit' name='loginSubmit'>Login</button>

        <!--this custom_captcha id is used in js file,you can change in js and html both-->
        <div id='custom_captcha'></div>

And finally, user_account where the login is processed:

if(isset($_POST['captcha_challenge']) && $_POST['captcha_challenge'] == $_SESSION['captcha_text']) {

Why does my table slowly recede in R Markdown when using the DT scrollResize plugin?

I ran the following code in R markdown. The table starts full size but then slowly recedes without me doing anything. Within a few seconds you can’t see any rows nor can you do anything to view them. I followed the format as shown here and there did not appear to be any additional information on the DT package website.

I’m using DT version 0.20, R version 4.1.2 and RStudio 2022.02.0+443.

    ---
    title: "scrollResize Not Working"
    output: html_document
    ---

    ```{r}

    library(DT)

    datatable(iris, plugins = "scrollResize",
          
          options = list(scrollResize = TRUE,
                         paging = FALSE,
                         scrollY = '300px',
                         scrollCollapse = TRUE))

    ```

How to use Cleanup in useEffect

I have the below code, where it pull all data from server as API

  export const apiExpenses = () => {

    //Get All Expenses, Realtime
    const { data: tableDataExpenses, error, mutate } = useSWR('/api/expenses/datatable', () =>
      axios
          .get('/api/expenses/datatable')
          .then(res => res.data),        
    )

    return {
        tableDataExpenses,
      }

  }

And using the output of that API in another component with useEffect

const { tableDataExpenses } = apiExpenses();
const [expenses, setExpenses] = useState([]);

useEffect(() => {
    setExpenses(tableDataExpenses.data);

    return () => {
      setExpenses([]);
    };
  }, [tableDataExpenses]);

But I am ending in below errors,

 Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

And because of this, I can’t get the value of tableDataExpenses

If I remove setExpenses from useEffect there are no errors and again if I write setExpenses it loads data.

But again in the next render, it fails with error.

So How can I prevent this error with the cleanup function?

I don’t understand why my code isn’t working [duplicate]

I have just started learning javascript and have made a small little program to create an alert when a button is clicked, yet it doesn’t work and I’m not sure why.

Here is my code:

<html>
<head>
    <style>
        body {
            background-color: grey;
            color: white;
        }
    </style>
    <script>
        const some_action = () => {
            window.alert("hi")
        }
        document.getElementById("btn").addEventListener("click", some_action)
    </script>
</head>
<body>
    <p>
        <button id="btn">click me</button>
    </p>
</body>

</html>

Writing image data to filesystem FS Electron Node.js

So I am trying to download an image. But everything I try it keeps giving me the same kind of error. Anyone got the same problem or solution why fs won’t work in fs.

Versions:
Node: 16.14.0
npm: 8.2.0
Electron: ^16.0.7

Current error:d.writeFile is not a function

My code:

// dependecies:
const axios = require('axios');
const fs = require('fs');


        image.promise = new Promise((resolve, reject) => {
          const formData = new FormData();
          formData.append('files', file);
          axios
            .post('http://api.resmush.it/', formData, { headers: { 'Content-Type': 'multipart/form-data' } })
            .then(response => {
              const url = response.data.dest;
              function downloadFile() {
                axios
                  .get(url)
                  .then(response => {
                    console.log(response)
                    console.log(response.data)
                    fs.writeFile("/tmp/test", "Hey there!", function(err) {
                          if(err) {
                              return console.log(err);
                          }
                          console.log("The file was saved!");
                      });
                      fs.writeFileSync('/tmp/test-sync', 'Hey there!');

                    })
                  // .then(response => resolve(response))
                  // .catch(() => reject());
                  .catch(reject => {console.log(reject)})
              }
              downloadFile();
            })
            .catch(() => reject());
        })

Allow curly brackets in url validation

I’m working with Yup and would like it to match urls that use curly brackets in the query string in cases like these (currently it doesn’t match as a valid url):

https://blah.blahblah.net/register/{0}
https://blah.blahblah.net/provider/tool.php?id={0}

I know that I need to have a custom regex match for this.

It would be great if the solution could be “built” on top of current Yup url regex match case which is:
/^((https?|ftp):)?//(((([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(%[da-f]{2})|[!$&'()*+,;=]|:)*@)?(((d|[1-9]d|1dd|2[0-4]d|25[0-5]).(d|[1-9]d|1dd|2[0-4]d|25[0-5]).(d|[1-9]d|1dd|2[0-4]d|25[0-5]).(d|[1-9]d|1dd|2[0-4]d|25[0-5]))|((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?)(:d*)?)(/((([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(%[da-f]{2})|[!$&'()*+,;=]|:|@)+(/(([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(%[da-f]{2})|[!$&'()*+,;=]|:|@)*)*)?)?(?((([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(%[da-f]{2})|[!$&'()*+,;=]|:|@)|[uE000-uF8FF]|/|?)*)?(#((([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(%[da-f]{2})|[!$&'()*+,;=]|:|@)|/|?)*)?$/i;