Categories
JavaScript Answers

How to get the current date in an app with Node.js?

Sometimes, we want to get the current date in an app with Node.js

In this article, we’ll look at how to get the current date in an app with Node.js.

How to get the current date in an app with Node.js?

To get the current date in an app with Node.js, we can use the Date constructor with no arguments.

For instance, we write

const datetime = new Date();
console.log(datetime);

to create a Date instance with the current datetime value.

And then we log the datetime.

Conclusion

To get the current date in an app with Node.js, we can use the Date constructor with no arguments.

Categories
JavaScript Answers

How to fix the ER_NOT_SUPPORTED_AUTH_MODE error when connection to MySQL server from a Node.js app with the mysql library?

Sometimes, we want to fix the ER_NOT_SUPPORTED_AUTH_MODE error when connection to MySQL server from a Node.js app with the mysql library.

In this article, we’ll look at how to fix the ER_NOT_SUPPORTED_AUTH_MODE error when connection to MySQL server from a Node.js app with the mysql library.

How to fix the ER_NOT_SUPPORTED_AUTH_MODE error when connection to MySQL server from a Node.js app with the mysql library?

To fix the ER_NOT_SUPPORTED_AUTH_MODE error when connection to MySQL server from a Node.js app with the mysql library, we call createConnection with the insecureAuth option set to true.

For instance, we write

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '********',
  database: 'bill_database',
  insecureAuth: true
});

to call createConnection to make the database connection to the MySQL server with an object with the credentials, database name and host, and some options.

In it, we set insecureAuth to true to let us connect to the database with any authentication mode.

Conclusion

To fix the ER_NOT_SUPPORTED_AUTH_MODE error when connection to MySQL server from a Node.js app with the mysql library, we call createConnection with the insecureAuth option set to true.

Categories
JavaScript Answers

How to query ISODate fields with Node.js MongoDB?

Sometimes, we want to query ISODate fields with Node.js MongoDB.

In this article, we’ll look at how to query ISODate fields with Node.js MongoDB.

How to query ISODate fields with Node.js MongoDB?

To query ISODate fields with Node.js MongoDB, we can use JavaScript date objects.

For instance, we write

const cursor = collection.find({
  title,
  dateCreated: {
    "$gte": new Date("2020-10-01T00:00:00.000Z"),
    "$lt": new Date("2022-03-13T16:17:36.470Z")
  }
});

to call find to query dateCreated greater than or equal to "2020-10-01T00:00:00.000Z" and less than "2022-03-13T16:17:36.470Z".

We create the date objects from the date strings by passing them into the Date constructor.

Conclusion

To query ISODate fields with Node.js MongoDB, we can use JavaScript date objects.

Categories
JavaScript Answers

How to check if Node.js writeFileSync successfully wrote the file?

Sometimes, we want to check if Node.js writeFileSync successfully wrote the file.

In this article, we’ll look at how to check if Node.js writeFileSync successfully wrote the file.

How to check if Node.js writeFileSync successfully wrote the file?

To check if Node.js writeFileSync successfully wrote the file, we should wrap writeFileSync in a try-catch block.

For instance, we write

try {
  fs.writeFileSync(file, content, 'utf8');
} catch (e) {
  conso.e.log(e)
}

to call writeFileSync to write the content of the file to the file path with 'utf8' encoding.

If the write operation fails, an error will be thrown, and the error will be caught by the catch block.

Conclusion

To check if Node.js writeFileSync successfully wrote the file, we should wrap writeFileSync in a try-catch block.

Categories
JavaScript Answers

How to check if a request is http or https in Node.js?

Sometimes, we want to check if a request is http or https in Node.js.

In this article, we’ll look at how to check if a request is http or https in Node.js.

How to check if a request is http or https in Node.js?

To check if a request is http or https in Node.js, we can use the req.secure property in our middlewares.

req.secure is a shorthand for req.protocol === 'https'.

And if our app is behind a proxy, we need to enable 'trust proxy' so that req.protocol reflects the protocol that’s used to communicate between the client and proxy.

To enable it, we write

app.enable('trust proxy');

Conclusion

To check if a request is http or https in Node.js, we can use the req.secure property in our middlewares.