Categories
JavaScript Answers

How to make multiple route parameters optional in Node.js Express?

Sometimes, we want to make multiple route parameters optional in Node.js Express.

In this article, we’ll look at how to make multiple route parameters optional in Node.js Express.

How to make multiple route parameters optional in Node.js Express?

To make multiple route parameters optional in Node.js Express, we can put a question mark after each optional route parameter placeholder.

For instance, we write

app.get('/articles/:year?/:month?/:day?', (req, res) => {
  const {
    year,
    month,
    day
  } = req.params
  //...
})

to call .app.get with a route path that has the optional year, month, and day parameter as indicated by the ? after each parameter.

Then we get the year, month, and day parameter value from req.params.

Conclusion

To make multiple route parameters optional in Node.js Express, we can put a question mark after each optional route parameter placeholder.

Categories
JavaScript Answers

How to format console output with Node.js?

Sometimes, we want to format console output with Node.js.

In this article, we’ll look at how to format console output with Node.js.

How to format console output with Node.js?

To format console output with Node.js, we can use the string padStart and padEnd methods.

For instance, we write

'abc'.padStart(10, "foo");

to prepend the 'abc' string with 'foo' until its length is 10.

So it returns "foofoofabc".

If we skip the 2nd argument, empty space will be prepended.

Also, we can append to a string until it reaches the given length with padEnd.

For instance, we write

'abc'.padEnd(10, "foo");

to call padEnd to append the 'abc' string with 'foo' until its length is 10.

So it returns "abcfoofoof".

If we skip the 2nd argument, empty space will be appended.

Conclusion

To format console output with Node.js, we can use the string padStart and padEnd methods.

Categories
JavaScript Answers

How to upload a file to Amazon S3 with Node.js?

Sometimes, we want to upload a file to Amazon S3 with Node.js.

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

How to upload a file to Amazon S3 with Node.js?

To upload a file to Amazon S3 with Node.js, we can use the putObject method.

For instance, we write

const AWS = require('aws-sdk');
AWS.config.update({
  accessKeyId: 'accessKeyId',
  secretAccessKey: 'secretAccessKey',
  region: 'region'
});

const upload = async () => {
  const params = {
    Bucket: "yourBucketName",
    Key: 'someUniqueKey',
    Body: 'someFile'
  };
  try {
    let uploadPromise = await new AWS.S3().putObject(params).promise();
    console.log("Successfully uploaded data to bucket");
  } catch (e) {
    console.log(e);
  }
}

to define the upload function that calls putObject with the params object to upload the file that’s set as the value of Body.

We set the Bucket and Key to set the bucket and key that we use to access the uploaded file.

And then we call promise to return a promise that resolves when the upload is done.

Conclusion

To upload a file to Amazon S3 with Node.js, we can use the putObject method.

Categories
JavaScript Answers

How to decompress a Node.js request’s module gzip response body?

Sometimes, we want to decompress a Node.js request’s module gzip response body.

In this article, we’ll look at how to decompress a Node.js request’s module gzip response body.

How to decompress a Node.js request’s module gzip response body?

To decompress a Node.js request’s module gzip response body, we can set the encoding option to null and then use the zlib.gunzip method.

For instance, we write

const request = require('request');
const zlib = require('zlib');

request(url, {
  encoding: null
}, (err, response, body) => {
  if (response.headers['content-encoding'] === 'gzip') {
    zlib.gunzip(body, (err, unzipped) => {
      callback(unzipped.toString());
    });
  } else {
    callback(body);
  }
});

to call request with an object that has encoding set to null.

Then in the callback, we check if the content-encoding response header is 'gzip'.

If it is, then we call zlib.gunzip to decompress the response body.

And then we can get the unzipped response contents from the unzipped parameter in the gunzip callback.

Conclusion

To decompress a Node.js request’s module gzip response body, we can set the encoding option to null and then use the zlib.gunzip method.

Categories
JavaScript Answers

How to destroy a cookie with Node.js and Express?

Sometimes, we want to destroy a cookie with Node.js and Express.

In this article, we’ll look at how to destroy a cookie with Node.js and Express.

How to destroy a cookie with Node.js and Express?

To destroy a cookie with Node.js and Express, we can use the res.clearCookie method.

For instance, we write

res.clearCookie("key");

to call clearCookie with the 'key' of the cookie to delete the cookie with key 'key'.

Conclusion

To destroy a cookie with Node.js and Express, we can use the res.clearCookie method.