Categories
JavaScript Answers Nodejs

How to decode base64 to hexadecimal string with JavaScript and Node.js?

Sometimes, we want to decode base64 to hexadecimal string with JavaScript and Node.js.

In this article, we’ll look at how to decode base64 to hexadecimal string with JavaScript and Node.js.

How to decode base64 to hexadecimal string with JavaScript and Node.js?

To decode base64 to hexadecimal string with JavaScript and Node.js, we can use the Buffer.from method to convert the base64 string to a buffer.

Then we can call the buffer’s toString method with 'hex' to convert it to a hex string.

For instance, we write:

const rawData = 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
const buffer = Buffer.from(rawData, 'base64');
const bufString = buffer.toString('hex');
console.log(bufString)

to call Buffer.from with the rawData base64 string and 'base64' to convert the rawData base64 string to a buffer.

Then we call buffer.toString with 'hex' to convert the buffer to a hex string.

As a result, we get that bufString is '75ab5a8a66a07bfa6781b6ac7bae22541391c3428682800000035252111480000001400000014201800000235bc9b940000007125110550235d8fe3fffcff0dfc1880170c804a1340c7c609633410003bd4d72f4638387c0000000125153912b909820'.

Conclusion

To decode base64 to hexadecimal string with JavaScript and Node.js, we can use the Buffer.from method to convert the base64 string to a buffer.

Then we can call the buffer’s toString method with 'hex' to convert it to a hex string.

Categories
JavaScript Answers Nodejs

How to get the ID parameter from the URL with Express and Node.js?

Sometimes, we want to get the ID parameter from the URL with Express and Node.js.

In this article, we’ll look at how to get the ID parameter from the URL with Express and Node.js.

How to get the ID parameter from the URL with Express and Node.js?

To get the ID parameter from the URL with Express and Node.js, we can get it from the req.params object.

For instance, we write:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.get('/user/:id/docs', (req, res) => {
  const { id } = req.params;
  res.send(id)
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

to add the /user/:id/docs route with:

app.get('/user/:id/docs', (req, res) => {
  const { id } = req.params;
  res.send(id)
});

':id' is the placeholder for the id URL parameter.

We get the ID value from req.params.

Therefore, if we go to /user/1/docs, we see 1 returned as the response.

Conclusion

To get the ID parameter from the URL with Express and Node.js, we can get it from the req.params object.

Categories
JavaScript Answers Nodejs

How to write in a text file without overwriting with Node.js?

Sometimes, we want to write in a text file without overwriting with Node.js.

In this article, we’ll look at how to write in a text file without overwriting with Node.js.

How to write in a text file without overwriting with Node.js?

To write in a text file without overwriting with Node.js, we can use the appendFile method.

For instance, we write:

const { promises: fs } = require("fs");

const write = async () => {
  await fs.appendFile("file.txt", 'abc')
}
write()

to call fs.appendFile with the path of the file to write to and the content to write into the file respectively.

The content will be added after the existing content of the file.

Conclusion

To write in a text file without overwriting with Node.js, we can use the appendFile method.

Categories
JavaScript Answers Nodejs

How to add to an existing JSON file in Node.js?

Sometimes, we want to add to an existing JSON file in Node.js.

In this article, we’ll look at how to add to an existing JSON file in Node.js.

How to add to an existing JSON file in Node.js?

To add to an existing JSON file in Node.js, we can use the fs.readFile method.

For instance, we have:

["foo","bar"]

in results.json

Then we write:

const { promises: fs } = require("fs");
const currentSearchResult = 'example'

const write = async () => {
  const data = await fs.readFile('results.json')
  const json = JSON.parse(data)
  json.push(currentSearchResult)
  await fs.writeFile("results.json", JSON.stringify(json))
}
write()

to call fs.readFile to read results.json into data.

Then we call JSON.parse with data to parse data into an onject.

Then we call json.push with currentSearchResult to add currentSearchResult to json assuming json is an array.

Finally, we call fs.writeFile with the file path and the content to write to the file.

Now result.json has:

["foo","bar","example"]

Conclusion

To add to an existing JSON file in Node.js, we can use the fs.readFile method.

Categories
JavaScript Answers Nodejs

How to copy a file with Node.js?

Sometimes, we want to copy a file with Node.js.

In this article, we’ll look at how to copy a file with Node.js.

How to copy a file with Node.js?

To copy a file with Node.js, we can use the read stream’s pipe method.

For instance, we write:

const fs = require('fs')
const oldFile = fs.createReadStream('./foo.txt');
const newFile = fs.createWriteStream('./bar.txt');
oldFile.pipe(newFile);

to call fs.createReadStream with the path of the file to copy.

Then we call fs.createWriteStream with the path of the file to copy to.

Then we call oldFile.pipe with newFile to copy the contents of ./foo.txt to ./bar.txt.

Conclusion

To copy a file with Node.js, we can use the read stream’s pipe method.