Categories
JavaScript Answers

How to send flash messages in Express and JavaScript?

Sometimes, we want to send flash messages in Express and JavaScript.

In this article, we’ll look at how to send flash messages in Express and JavaScript.

How to send flash messages in Express and JavaScript?

To send flash messages in Express and JavaScript, we use the connect-flash package.

To install it, we run

npm i connect-flash

Then we use it by writing

const express = require("express");
const flash = require("connect-flash");

const app = express();

app.use(flash());

//...

app.all("/", (req, res) => {
  req.flash("test", "it worked");
  res.redirect("/test");
});

We add the flash middleware with app.use.

Then we send our flash message with req.flash.

Conclusion

To send flash messages in Express and JavaScript, we use the connect-flash package.

Categories
JavaScript Answers

How to create string with multiple spaces in JavaScript?

Sometimes, we want to create string with multiple spaces in JavaScript.

In this article, we’ll look at how to create string with multiple spaces in JavaScript.

How to create string with multiple spaces in JavaScript?

To create string with multiple spaces in JavaScript, we use &nbsp.

For instance, we write

const str = "hello" + "&nbsp &nbsp &nbsp &nbsp &nbsp" + "world";

to add 5 spaces to the str string with "&nbsp &nbsp &nbsp &nbsp &nbsp".

Conclusion

To create string with multiple spaces in JavaScript, we use &nbsp.

Categories
JavaScript Answers

How to set image to fit width of the page using jsPDF and JavaScript?

Sometimes, we want to set image to fit width of the page using jsPDF and JavaScript.

In this article, we’ll look at how to set image to fit width of the page using jsPDF and JavaScript.

How to set image to fit width of the page using jsPDF and JavaScript?

To set image to fit width of the page using jsPDF and JavaScript, we call the addImage method.

For instance, we write

const doc = new jsPDF("p", "mm", "a4");

const width = doc.internal.pageSize.getWidth();
const height = doc.internal.pageSize.getHeight();
const imgData = "data:image/jpeg;base64,/9j/4AAQSkZJ......";

doc.addImage(imgData, "JPEG", 0, 0, width, height);

to get the width and height of the page with getWidth and getHeight.

Then we call addImage with imgData, width and height to add the image with the given width and height.

0 and 0 are the x and y coordinates of the top left corner of the image.

Conclusion

To set image to fit width of the page using jsPDF and JavaScript, we call the addImage method.

Categories
JavaScript Answers

How to call a JavaScript function in a click event handler?

Sometimes, we want to call a JavaScript function in a click event handler.

In this article, we’ll look at how to call a JavaScript function in a click event handler.

How to call a JavaScript function in a click event handler?

To call a JavaScript function in a click event handler, we call the function we want in the click handler.

For instance, we write

const f1 = () => {
  console.log("f1 called");
};

window.onload = () => {
  document.getElementById("save").onclick = () => {
    f1();
  };
};

to get the element with ID save with getElementById.

In it, we set its onclick property to a function that calls f1 when the element is clicked.

Conclusion

To call a JavaScript function in a click event handler, we call the function we want in the click handler.

Categories
JavaScript Answers

How to access microphone from a browser with JavaScript?

Sometimes, we want to access microphone from a browser with JavaScript.

In this article, we’ll look at how to access microphone from a browser with JavaScript.

How to access microphone from a browser with JavaScript?

To access microphone from a browser with JavaScript, we use the navigator.mediaDevices.getUserMedia method.

For instance, we write

const getMedia = async (constraints) => {
  let stream = null;
  try {
    stream = await navigator.mediaDevices.getUserMedia(constraints);
    console.log(stream);
  } catch (err) {
    document.write(err);
  }
};

getMedia({ audio: true, video: true });

to define the getMedia function.

In it, we call navigator.mediaDevices.getUserMedia with the constraints object with the options for what to get permission for.

It returns a promise with the stream.

We call getMedia with { audio: true, video: true } to get access to the microphone and camera.

Conclusion

To access microphone from a browser with JavaScript, we use the navigator.mediaDevices.getUserMedia method.