Categories
JavaScript Answers

How to print a list of all installed Node.js modules?

Sometimes, we want to print a list of all installed Node.js modules.

In this article, we’ll look at how to print a list of all installed Node.js modules.

How to print a list of all installed Node.js modules?

To print a list of all installed Node.js modules, we can use npm ls.

For instance, we run

npm -g ls --depth=0

to print all the installed global packages.

And we run

npm ls --depth=0

to print all the installed local packages in a folder.

Conclusion

To print a list of all installed Node.js modules, we can use npm ls.

Categories
JavaScript Answers

How to use Nodemailer with Gmail and Node.js?

Sometimes, we want to use Nodemailer with Gmail and Node.js.

In this article, we’ll look at how to use Nodemailer with Gmail and Node.js.

How to use Nodemailer with Gmail and Node.js?

To use Nodemailer with Gmail and Node.js, we use the createTransport and sendMail methods.

For instance, we write

const mailer = require("nodemailer");
const smtpTransport = mailer.createTransport("SMTP", {
  service: "Gmail",
  auth: {
    user: "abc@gmail.com",
    pass: "password"
  }
});

const mail = {
  from: "from <from@gmail.com>",
  to: "to@gmail.com",
  subject: "Send Email Using Node.js",
  text: "Node.js",
  html: "<b>Node.js</b>"
}

smtpTransport.sendMail(mail, (error, response) => {
  if (error) {
    console.log(error);
  } else {
    console.log(response.message);
  }
  smtpTransport.close();
});

to call mailer.createTransport to create the smtpTransport object that we use to log into our SMTP server.

Then we call smtpTransform.sendMail to send our email with the data coming from the mail object.

If there’s an error, then error is set in the callback.

Otherwise, response is set.

Finally, we log out of the SMTP server with smtpTransport.close.

Conclusion

To send emails in Node.js, we can use the nodemailer package.

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.