Categories
JavaScript Answers Nodejs

How to get a directory from a file path or URL with JavaScript?

Sometimes, we want to get a directory from a file path or URL with JavaScript.

In this article, we’ll look at how to get a directory from a file path or URL with JavaScript.

How to get a directory from a file path or URL with JavaScript?

To get a directory from a file path or URL with JavaScript, we can use the path.dirname method.

For instance, we write:

const path = require('path')
const dirName = path.dirname('/this/is/a/path/to/a/file');
console.log(dirName)

We call path.dirname with a path to get the directory path.

Therefore, dirName is '/this/is/a/path/to/a'.

Conclusion

To get a directory from a file path or URL with JavaScript, we can use the path.dirname method.

Categories
JavaScript Answers Nodejs

How to read a 3 bytes Buffer as an integer with JavaScript and Node.js?

Sometimes, we want to read a 3 bytes Buffer as an integer with JavaScript and Node.js.

In this article, we’ll look at how to read a 3 bytes Buffer as an integer with JavaScript and Node.js.

How to read a 3 bytes Buffer as an integer with JavaScript and Node.js?

To read a 3 bytes Buffer as an integer with JavaScript and Node.js, we can use the readUintBE method.

For instance, we write:

const buffer = Buffer.from('hello world', 'utf8');
const decimal = buffer.readUIntBE(0, 3);
console.log(decimal)

to create a buffer with Buffer.from.

Then we call buffer.readUIntBE with 0 and 3 to read the 3 bytes and convert it into a decimal number.

Therefore, decimal is 6841708.

Conclusion

To read a 3 bytes Buffer as an integer with JavaScript and Node.js, we can use the readUintBE method.

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.