Categories
JavaScript Answers

How to URl encode strings in Node.js?

Sometimes, we want to URl encode strings in Node.js.

In this article, we’ll look at how to URl encode strings in Node.js.

How to URl encode strings in Node.js?

To URl encode strings in Node.js, we can call encodeURIComponent.

For instance, we write

encodeURIComponent('select * from table where uid = me()')

to call encodeURIComponent with return a string that is URI encoded.

Conclusion

To URl encode strings in Node.js, we can call encodeURIComponent.

Categories
JavaScript Answers

How to fix ‘ReferenceError: fetch is not defined’ error in Node.js?

Sometimes, we want to fix ‘ReferenceError: fetch is not defined’ error in Node.js.

In this article, we’ll look at how to fix ‘ReferenceError: fetch is not defined’ error in Node.js.

How to fix ‘ReferenceError: fetch is not defined’ error in Node.js?

To fix ‘ReferenceError: fetch is not defined’ error in Node.js, we can install node-fetch.

To install it, we run

npm i node-fetch

Then we import it with

import fetch from "node-fetch";

so we can call fetch.

Conclusion

To fix ‘ReferenceError: fetch is not defined’ error in Node.js, we can install node-fetch.

Categories
JavaScript Answers

How to measure the execution time of Node.js code with callbacks?

Sometimes, we want to measure the execution time of Node.js code with callbacks.

In this article, we’ll look at how to measure the execution time of Node.js code with callbacks.

How to measure the execution time of Node.js code with callbacks?

To measure the execution time of Node.js code with callbacks, we can use the process.hrtime method.

For instance, we write

let start = process.hrtime();

const elapsedTime = (note) => {
  const precision = 3;
  const [startTime, endTime] = process.hrtime(start)
  const elapsed = endTime / 1000000;
  console.log(startTime, elapsed);
  start = process.hrtime();
}

to create the elaspedTime function tyat calls process.hrtime with start to return the timestamp of the elasped time and assign that to endTime.

The endTime is in nanoseconds.

So we divide endTime by 1000000 to get the elasped time in seconds.

Conclusion

To measure the execution time of Node.js code with callbacks, we can use the process.hrtime method.

Categories
JavaScript Answers

How to create a hash string with Node.js?

Sometimes, we want to create a hash string with Node.js.

In this article, we’ll look at how to create a hash string with Node.js.

How to create a hash string with Node.js?

To create a hash string with Node.js, we can use the crypto module.

For instance, we write

const crypto = require('crypto');
const name = 'hello';
const hash = crypto.createHash('md5').update(name).digest('hex');

to call crypto.createHash with 'md5' to create a new hash string.

And we call update to create the hash string hashed from name.

Finally, we call digest with 'hex' to return the hashed string as hex.

Conclusion

To create a hash string with Node.js, we can use the crypto module.

Categories
JavaScript Answers

How to set environment variables from within package.json?

Sometimes, we want to set environment variables from within package.json.

In this article, we’ll look at how to set environment variables from within package.json.

How to set environment variables from within package.json?

To set environment variables from within package.json, we can put the keys and values of the environment variables before the command we run.

For instance, we write

...
"scripts": {
  "start": "node app.js",
  "test": "NODE_ENV=test mocha --reporter spec"
},
...

to add the test script that sets the NODE_ENV environment variable to test.

And then we run the mocha command with the environment variable set.

Conclusion

To set environment variables from within package.json, we can put the keys and values of the environment variables before the command we run.