Categories
JavaScript Answers

How to salt and hash password in Node with crypto?

To salt and hash password in Node with crypto, we call the hashSync method.

For instance, we write

const bcrypt = require("bcrypt");
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync("B4c0//", salt);

to create a salt with genSaltSync.

Then we call hashSync with the string to create a hash for and the salt to create the hash.

We then compare the hash with

const isSame = bcrypt.compareSync("B4c0//", hash);

by calling compareSync with the unhashed string with the hash.

Categories
JavaScript Answers

How to delete existing text from input using Node Puppeteer?

To delete existing text from input using Node Puppeteer, we set the value of the input to an empty string.

For instance, we write

await page.evaluate(() => (document.getElementById("inputID").value = ""));

to call page.evaluate with a callback that gets the input with getElementById.

And we set the value property to an empty string to empty the input.

Categories
JavaScript Answers

How to wait for a child process to finish in Node.js?

To wait for a child process to finish in Node.js, we listen for the exit event.

For instance, we write

const child = require("child_process").exec("python celulas.py");

child.stdout.pipe(process.stdout);
child.on("exit", () => {
  process.exit();
});

to call on with 'exit' to listen for the exit event, which is emitted when the process is finished.

Categories
JavaScript Answers

How to fix create-react-app, installation error (“command not found”) with JavaScript?

To fix create-react-app, installation error ("command not found") with JavaScript, we run npx.

For instance, we run

npx create-react-app app_name

to create a React project with name app_name with npx create-react-app without installing create-react-app locally.

Categories
JavaScript Answers

How to find the a referring sites URL in Node?

To find the a referring sites URL in Node, we call the req.headers.referer property.

For instance, we write

const referrer = req.headers.referer;

to get the referrer with the req.headers.referer property.