Categories
JavaScript Answers

How to send emails in Node.js?

Sometimes, we want to send emails in Node.js.

In this article, we’ll look at how to send emails in Node.js.

How to send emails in Node.js?

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

To install it, we run

npm i nodemailer

Then we use it by writing

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();
});

we 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 share constants in Node.js modules?

Sometimes, we want to share constants in Node.js modules.

In this article, we’ll look at how to share constants in Node.js modules.

How to share constants in Node.js modules?

To share constants in Node.js modules, we can put them all in a module and export them.

For instance, we write

./constants.js

module.exports = Object.freeze({
  MY_CONSTANT: 'some value',
  ANOTHER_CONSTANT: 'another value'
});

to set module.exports to a frozen object with some properties inside with the constant values.

We freeze the object with Object.freeze so it can’t be modified accidentally.

Then we can import the constants with

./app.js

const constants = require('./constants');

console.log(constants.MY_CONSTANT);
console.log(constants.ANOTHER_CONSTANT);

We call require with the path to ./constants.js.

Then we get the values from constants.

Conclusion

To share constants in Node.js modules, we can put them all in a module and export them.

Categories
JavaScript Answers

How to make a function wait until a callback has been called using Node.js?

Sometimes, we want to make a function wait until a callback has been called using Node.js.

In this article, we’ll look at how to make a function wait until a callback has been called using Node.js.

How to make a function wait until a callback has been called using Node.js?

To make a function wait until a callback has been called using Node.js, we can run our code in the callback that’s called when the operation is done.

For instance, we write

const f = (query, callback) => {
  myApi.exec('SomeCommand', (response) => {
    callback(response);
  });
}

to define the function f that takes the callback parameter.

We call that in the callback that’s called when myApi.exec is done.

We call callback with response as the argument to pass response to the callback.

Then we can call f to get the response value with

f(query, (returnValue) => {
  // ...
});

We get response from the returnValue.

Conclusion

To make a function wait until a callback has been called using Node.js, we can run our code in the callback that’s called when the operation is done.

Categories
JavaScript Answers

How to render basic HTML view with Express?

Sometimes, we want to render basic HTML view with Express.

In this article, we’ll look at how to render basic HTML view with Express.

How to render basic HTML view with Express?

To render basic HTML view with Express, we call app.use with express.static to serve a static folder.

For instance, we write

const express = require('express');
const app = express.createServer();

app.set("view options", {
  layout: false
});
app.use(express.static(__dirname + '/public'));

app.get('/', (req, res) => {
  res.render('index.html');
});

app.listen(8080, '127.0.0.1')

to call express.static with the static folder path in our project to serve that as the static folder.

Then we call res.render with a path to a file in the static folder to render that as the view content.

Conclusion

To render basic HTML view with Express, we call app.use with express.static to serve a static folder.

Categories
JavaScript Answers

How to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js?

Sometimes, we want to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js.

In this article, we’ll look at how to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js.

How to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js?

To format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js, we can use the toISOString method.

For instance, we write

new Date().toISOString()
  .replace(/T/, ' ')
  .replace(/\..+/, '')

to call toISOString on a Date instance.

Then we replace the T and the periods with a space and empty string respectively.

Conclusion

To format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js, we can use the toISOString method.