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.

Categories
JavaScript Answers

How to remove a directory which is not empty with Node.js?

Sometimes, we want to remove a directory which is not empty with Node.js.

In this article, we’ll look at how to remove a directory which is not empty with Node.js.

How to remove a directory which is not empty with Node.js?

To remove a directory which is not empty with Node.js, we can use the rm or rmSync method.

For instance, we write

const fs = require('fs');

fs.rm('/path/to/delete', {
  recursive: true
}, () => console.log('done'));

to call fs.rm with the path to delete as the first argument.

We set recursive to true in the object in the 2nd argument to let us delete an non-empty directory.

The 3rd argument is a callback that runs when rm is done.

rm will delete the directory asynchronously.

We call rmSync to delete a non-empty directory synchronously.

For instance, we write

const fs = require('fs');
fs.rmSync('/path/to/delete', {
  recursive: true
});
console.log('done');

to call rmSync with the path of the directory to delete and the same object as the arguments.

Conclusion

To remove a directory which is not empty with Node.js, we can use the rm or rmSync method.

Categories
JavaScript Answers

How to stringify an Error using JSON.stringify?

Sometimes, we want to stringify an Error using JSON.stringify.

In this article, we’ll look at how to stringify an Error using JSON.stringify.

How to stringify an Error using JSON.stringify?

To stringify an Error using JSON.stringify, we can call inspect.

For instance, we write

import {
  inspect
} from "util";

const myObject = new Error("This is error");
console.log(inspect(myObject, {
  depth: null
}));

to call inspect with the myObject error object.

The 2nd argument is an object with depth set to null, which lets us stringify all nested properties in the myObject error object.

Conclusion

To stringify an Error using JSON.stringify, we can call inspect.