Categories
JavaScript Answers

How to zip an entire directory using Node.js?

Sometimes, we want to zip an entire directory using Node.js.

In this article, we’ll look at how to zip an entire directory using Node.js.

How to zip an entire directory using Node.js?

To zip an entire directory using Node.js, we can use the archiver package.

We install the package by running

npm i archiver

Then we write

const fs = require('fs');
const archiver = require('archiver');

const output = fs.createWriteStream('target.zip');
const archive = archiver('zip');

output.on('close', () => {
  console.log(archive.pointer() + ' total bytes');
});

archive.on('error', (err) => {
  throw err;
});

archive.pipe(output);
archive.directory(sourceDir, false);
archive.directory('subdir/', 'new-subdir');

archive.finalize();

to call fs.createWriteStream with the path to the zip file.

And then we call archiver with 'zip' to create a zip file.

We call archive.pipe with output to pipe the content directory content to the zip file.

Then we call archive.directory with the directories we want to put in the zip file.

We call output.on with 'close' to run the callback when the write stream is done.

And we call archive.on with 'error' to listen for errors when making the zip file.

Finally, we call archive.finalize to finish the archiving operation.

Conclusion

To zip an entire directory using Node.js, we can use the archiver package.

Categories
JavaScript Answers

How to use HTML as the view engine in Express?

Sometimes, we want to use HTML as the view engine in Express.

In this article, we’ll look at how to use HTML as the view engine in Express.

How to use HTML as the view engine in Express?

To use HTML as the view engine in Express, we can use the expres.static method.

For instance, we write

app.use(express.static(__dirname + '/public'));

to call express.static with the path to the HTML files we want to serve.

We don’t need a view engine to render the HTML.

Conclusion

To use HTML as the view engine in Express, we can use the expres.static method.

Categories
JavaScript Answers

How to POST data with request module on Node.js?

Sometimes, we want to POST data with request module on Node.js.

In this article, we’ll look at how to POST data with request module on Node.js.

How to POST data with request module on Node.js?

To POST data with request module on Node.js, we can use the request.post method.

For instance, we write

const request = require('request');
request.post({
  headers: {
    'content-type': 'application/x-www-form-urlencoded'
  },
  url: 'http://localhost/test2.php',
  body: "mes=heydude"
}, (error, response, body) => {
  console.log(body);
});

to call request.post with an object that has the request headers, url, and request body.

The callback in the 2nd argument runs when the request is finished.

We get the response body from body.

Conclusion

To POST data with request module on Node.js, we can use the request.post method.

Categories
JavaScript Answers

How to stop Mongoose from adding an s at the end of a collection name?

Sometimes, we want to stop Mongoose from adding an s at the end of a collection name.

In this article, we’ll look at how to stop Mongoose from adding an s at the end of a collection name.

How to stop Mongoose from adding an s at the end of a collection name?

To stop Mongoose from adding an s at the end of a collection name, we can set the collection property when we create the schema.

For instance, we write

const dataSchema = new Schema({
  //..
}, {
  collection: 'data'
})

to create a Schema instance with an object that sets the collection property.

The collection property is set to the name we want for the collection.

Conclusion

To stop Mongoose from adding an s at the end of a collection name, we can set the collection property when we create the schema.

Categories
JavaScript Answers

How to remove all files from directory without removing directory in Node.js?

Sometimes, we want to remove all files from directory without removing directory in Node.js.

In this article, we’ll look at how to remove all files from directory without removing directory in Node.js.

How to remove all files from directory without removing directory in Node.js?

To remove all files from directory without removing directory in Node.js, we can use the readdir method to get an array of file paths in the directory.

Then we can use unlink to remove all the files.

For instance, we write

const fs = require('fs');
const path = require('path');

const directory = 'test';

fs.readdir(directory, (err, files) => {
  if (err) {
    throw err;
  }

  for (const file of files) {
    fs.unlink(path.join(directory, file), err => {
      if (err) {
        throw err;
      }
    });
  }
});

to call readdir to get an array of files.

Then we loop through the files in the for-of loop.

In the loop body, we call fs.unlink with the path of each file returned by path.join to remove them.

Conclusion

To remove all files from directory without removing directory in Node.js, we can use the readdir method to get an array of file paths in the directory.

Then we can use unlink to remove all the files.