Categories
JavaScript Answers

How to overwrite a file using fs in Node.js?

Sometimes, we want to overwrite a file using fs in Node.js.

In this article, we’ll look at how to overwrite a file using fs in Node.js.

How to overwrite a file using fs in Node.js?

To overwrite a file using fs in Node.js, we can call writeFile or writeFileSync with the 'w' flag.

For instance, we write

fs.writeFileSync(path, content, {
  encoding: 'utf8',
  flag: 'w'
})

to call writeFileSync with the file path, file content, and the 'w' flag to write the file even if it already exists.

We can pass in the same arguments with writeFile to write the file asynchronously.

Conclusion

To overwrite a file using fs in Node.js, we can call writeFile or writeFileSync with the 'w' flag.

Categories
JavaScript Answers

How to write an image to a local server with Node.js?

Sometimes, we want to write an image to a local server with Node.js.

In this article, we’ll look at how to write an image to a local server with Node.js.

How to write an image to a local server with Node.js?

To write an image to a local server with Node.js, we can use the fs.writeFile method.

For instance, we write

const http = require('http')
const fs = require('fs')

const options = {
  host: 'www.google.com',
  port: 80,
  path: '/images/logos/ps_logo2.png'
}

const request = http.get(options, (res) => {
  let imageData = ''
  res.setEncoding('binary')

  res.on('data', (chunk) => {
    imageData += chunk
  })

  res.on('end', () => {
    fs.writeFile('logo.png', imageData, 'binary', (err) => {
      if (err) {
        throw err
      }
      console.log('File saved.')
    })
  })
})

to call res.on with 'data' to listen for image data from the request we made with http.get.

We concatenate the chunks to imageData to combine the response data retrieved from the res stream.

And then we call res.on with 'end' to listen for the end event.

In the callback, we call writeFile with the imageData and 'binary' to write the file as binary data to logo.png.

The end event is emitted when the data is done reading.

Conclusion

To write an image to a local server with Node.js, we can use the fs.writeFile method.

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.