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.

Categories
JavaScript Answers

How to pretty-print JSON using Node.js?

Sometimes, we want to pretty-print JSON using Node.js.

In this article, we’ll look at how to pretty-print JSON using Node.js.

How to pretty-print JSON using Node.js?

To pretty-print JSON using Node.js, we can call JSON.stringify with the number of spaces to indent the JSON by.

For instance, we write

const fs = require('fs');

fs.writeFile('test.json', JSON.stringify({
  a: 1,
  b: 2,
  c: 3
}, null, 2));

to call fs.writeFile with the file path and the stringified JSON.

We stringify the JSON by calling JSON.stringify with the object to stringify as the first argument and the number of spaces to indent each level by as the 3rd argument.

As a result, the JSON written to test.json should be indented with 2 spaces in each level.

Conclusion

To pretty-print JSON using Node.js, we can call JSON.stringify with the number of spaces to indent the JSON by.

Categories
JavaScript Answers

How to require all files in a folder with Node.js?

Sometimes, we want to require all files in a folder with Node.js.

In this article, we’ll look at how to require all files in a folder with Node.js.

How to require all files in a folder with Node.js?

To require all files in a folder with Node.js, we can create an index.js file that exports all the files we want to require.

Then we can require the index.js file.

For instance, we write

routes/index.js

exports.something = require("./routes/something.js");
exports.others = require("./routes/others.js");

to export the ./routes/something.js and ./routes/others.js modules.

Then we can require the routes/index.js file with

app.js

const routes = require("./routes");

routes has the something and others properties.

routes.something references require("./routes/something.js") and routes.others references require("./routes/others.js").

Conclusion

To require all files in a folder with Node.js, we can create an index.js file that exports all the files we want to require.

Then we can require the index.js file.

Categories
JavaScript Answers

How to get a URL parameter in Express and Node.js?

Sometimes, we want to get a URL parameter in Express and Node.js.

In this article, we’ll look at how to get a URL parameter in Express and Node.js.

How to get a URL parameter in Express and Node.js?

To get a URL parameter in Express and Node.js, we can use the req.params property.

For instance, we write

app.get('/p/:tagId', (req, res) => {
  res.send(req.params.tagId);
});

to get the tagId URL parameter with req.params.tagId in the GET route’s route handler.

Then if we make a GET reqest to /p/1, we see that req.params.tagId is 1.

Conclusion

To get a URL parameter in Express and Node.js, we can use the req.params property.