Categories
JavaScript Answers

How to stream response in Express and JavaScript?

Sometimes, we want to stream response in Express and JavaScript.

In this article, we’ll look at how to stream response in Express and JavaScript.

How to stream response in Express and JavaScript?

To stream response in Express and JavaScript, we use res.write.

For instance, we write

app.get("/report", (req, res) => {
  res.write("USERID,NAME,FBID,ACCOUNT,SUBSCRIPTION,PRICE,STATE,TIMEPERIOD\n");

  for (let i = 0; i < 10; i++) {
    res.write("23,John Doe,1234,500,SUBSCRIPITON,100,ACTIVE,30\n");
  }

  res.end();
});

to call res.write to put each line in the response.

Then we call res.end to stop sending the lines.

Conclusion

To stream response in Express and JavaScript, we use res.write.

Categories
JavaScript Answers

How to define a regular expression to validate US phone numbers with JavaScript?

Sometimes, we want to define a regular expression to validate US phone numbers with JavaScript.

In this article, we’ll look at how to define a regular expression to validate US phone numbers with JavaScript.

How to define a regular expression to validate US phone numbers with JavaScript?

To define a regular expression to validate US phone numbers with JavaScript, we write a regex to match the groups of numbers.

For instance, we write

const re = /^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/;

to use [0-9]{3} to match the groups of 3 digits.

We use [0-9]{4} to match the group of 4 digits.

^ matches start of the string.

$ matches end of the string.

Conclusion

To define a regular expression to validate US phone numbers with JavaScript, we write a regex to match the groups of numbers.

Categories
JavaScript Answers

How to fix JSON.parse unexpected token s error with JavaScript?

Sometimes, we want to fix JSON.parse unexpected token s error with JavaScript.

In this article, we’ll look at how to fix JSON.parse unexpected token s error with JavaScript.

How to fix JSON.parse unexpected token s error with JavaScript?

To fix JSON.parse unexpected token s error with JavaScript, we should make sure strings are in double quotes.

For instance, we write

const s = '"something"';
const result = JSON.parse(s);

to wrap ‘something’ in double quotes to make it a valid JSON string.

Then we call JSON.parse with s to parse it into a JavaScript string.

Conclusion

To fix JSON.parse unexpected token s error with JavaScript, we should make sure strings are in double quotes.

Categories
JavaScript Answers

How to define a regular expression to match exactly 5 digits with JavaScript?

Sometimes, we want to define a regular expression to match exactly 5 digits with JavaScript.

In this article, we’ll look at how to define a regular expression to match exactly 5 digits with JavaScript.

How to define a regular expression to match exactly 5 digits with JavaScript?

To define a regular expression to match exactly 5 digits with JavaScript, we can use the \d pattern.

For instance, we write

const str = "f 34 545 323 12345 54321 123456";
const matches = str.match(/\b\d{5}\b/g);

console.log(matches);

to define the /\b\d{5}\b/g pattern that matches groups of 5 digits.

\b matches word bounddaries.

And \d{5} matches 5 digits.

Then we call str.match with the regex to return an array of all matches.

Conclusion

To define a regular expression to match exactly 5 digits with JavaScript, we can use the \d pattern.

Categories
JavaScript Answers

How to find the length or size of an array in JavaScript?

To find the length or size of an array in JavaScript, we use the length property.

For instance, we write

const arr = [];
testvar[1] = 2;
testvar[2] = 3;
console.log(arr.length);

to define the arr array.

And we set values for indexes 1 and 2.

Finally, we get the array size or length with the arr.length property.