Categories
JavaScript Answers

How to get the full URL in Express and Node.js?

Sometimes, we want to get the full URL in Express and Node.js.

In this article, we’ll look at how to get the full URL in Express and Node.js.

to get the full URL in Express and Node.js

To get the full URL in Express and Node.js, we can use the url package.

To install it, we run

npm i url

Then we can use it by writing

const url = require('url');

const fullUrl = (req) => {
  return url.format({
    protocol: req.protocol,
    host: req.get('host'),
    pathname: req.originalUrl
  });
}

to define the fullUrl function that takes the Express request req object.

And then we call url.format with an object that has the protocol, host, and pathnameset from the URL components inreq`.

protocol has the protocol like http or ftp.

host has the first segment of the URL after the protocol.

And pathname has the rest of the URL.

Conclusion

To get the full URL in Express and Node.js, we can use the url package.

Categories
JavaScript Answers

How to read a file one line at a time in Node.js?

Sometimes, we want to read a file one line at a time in Node.js.

In this article, we’ll look at how to read a file one line at a time in Node.js.

How to read a file one line at a time in Node.js?

To read a file one line at a time in Node.js, we can call thge fs.createReadStream method.

For instance, we write

const fs = require('fs');
const readline = require('readline');

const processLineByLine = async () => {
  const fileStream = fs.createReadStream('input.txt');
  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });

  for await (const line of rl) {
    console.log(`Line from file: ${line}`);
  }
}

processLineByLine();

to define the processLineByLine function that calls fs.createReadStream with the path of the file to read.

Then we call readline.createInterface to read the file line by line by setting the input to fileStream and crlfDelay to Infinity.

crlfDelay is set to recognize all instacnes of the CR and LF characters as a single line break.

And then we loop through each line in rl that’s read.

Conclusion

To read a file one line at a time in Node.js, we can call thge fs.createReadStream method.

Categories
JavaScript Answers

How to remove a file with Node.js?

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

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

How to remove a file with Node.js?

To remove a file with Node.js, we can use the fs.unlink` method.

For instance, we write

fs.unlink('./server/my.csv', (err) => {
  if (err) {
    return console.log(err);
  }
  console.log('file deleted successfully');
});

to call fs.unlink with the path of the file to delete and a callback that runs when unlink is finished.

err is set when there’s an error.

We can also call unlinkSync to delete a file synchronously.

For instance, we write

const fs = require('fs');
const filePath = './server/my.csv';
fs.unlinkSync(filePath);

to call unlinkSync with the filePath of the file to delete.

Conclusion

To remove a file with Node.js, we can use the fs.unlink` method.

Categories
JavaScript Answers

How to store Node.js deployment settings/configuration files?

Sometimes, we want to store Node.js deployment settings/configuration files.

In this article, we’ll look at how to store Node.js deployment settings/configuration files.

How to store Node.js deployment settings/configuration files?

To store Node.js deployment settings/configuration files, we can use the dotenv package.

To install it, we run

npm i dotenv

Then we write

const dotenv = require('dotenv');
dotenv.load();

to require the dotenv package.

And then we call dotenv.load to load our config file.

Then in our .env file, we can put some config entries like

S3_BUCKET=bucket
SECRET_KEY=secret
OTHER_SECRET_STUFF=secret

And the data from the .env file will be put in the process.env object with the keys in .env as the property names.

Conclusion

To store Node.js deployment settings/configuration files, we can use the dotenv package.

Categories
JavaScript Answers

How to call async/await functions in parallel with Node.js?

Sometimes, we want to call async/await functions in parallel with Node.js.

In this article, we’ll look at how to call async/await functions in parallel with Node.js.

How to call async/await functions in parallel with Node.js?

To call async/await functions in parallel with Node.js, we can call Promise.all.

For instance, we write

await Promise.all([someCall(), anotherCall()]);

to call Promise.all with an array of promises.

We use await to wait for all promises to resolve until we move onto the next line.

And we can store the resolved values with

const [someResult, anotherResult] = await Promise.all([someCall(), anotherCall()]);

Conclusion

To call async/await functions in parallel with Node.js, we can call Promise.all.