Categories
JavaScript Answers

How to get the redirected URL from the Node.js request module?

Sometimes, we want to get the redirected URL from the Node.js request module.

In this article, we’ll look at how to get the redirected URL from the Node.js request module.

How to get the redirected URL from the Node.js request module?

To get the redirected URL from the Node.js request module, we can get the redirected URL from res.headers.location property.

For instance, we write

const url = 'http://www.google.com';

request({
  url,
  followRedirect: false
}, (err, res, body) => {
  console.log(res.headers.location);
});

to call request with an object that has the followRedirect property set to true to follow redirects.

Then we can get the redirected URL from res.headers.location in the request callback after the request is done.

Conclusion

To get the redirected URL from the Node.js request module, we can get the redirected URL from res.headers.location property.

Categories
JavaScript Answers

How to modify the Node.js request default timeout time?

Sometimes, we want to modify the Node.js request default timeout time.

In this article, we’ll look at how to modify the Node.js request default timeout time.

How to modify the Node.js request default timeout time?

To modify the Node.js request default timeout time, we can set the timeout option when we call request.

For instance, we write

const options = {
  url: 'http://example.com',
  timeout: 120000
}

request(options, (err, resp, body) => {
  //...
});

to call request with options.

We set the timeout option to set the request timeout to 120000 milliseconds.

Conclusion

To modify the Node.js request default timeout time, we can set the timeout option when we call request.

Categories
JavaScript Answers

How to add a class conditionally to div inline with Jade?

Sometimes, we want to add a class conditionally to div inline with Jade.

In this article, we’ll look at how to add a class conditionally to div inline with Jade.

How to add a class conditionally to div inline with Jade?

To add a class conditionally to div inline with Jade, we can use a ternary expression.

For instance, we write

div#demo.collapse(class=typeof fromEdit === "undefined" ? undefined : "in")

to check if fromEdit is undefined with typeof fromEdit === "undefined".

If it is, we set the class attribute to undefined.

Otherwise, we set it to in.

Conclusion

To add a class conditionally to div inline with Jade, we can use a ternary expression.

Categories
JavaScript Answers

How to set uploaded file and folder permissions with AWS S3 Node.js SDK?

Sometimes, we want to set uploaded file and folder permissions with AWS S3 Node.js SDK.

In this article, we’ll look at how to set uploaded file and folder permissions with AWS S3 Node.js SDK.

How to set uploaded file and folder permissions with AWS S3 Node.js SDK?

To set uploaded file and folder permissions with AWS S3 Node.js SDK, we can set the ACL property of the object that we call putObject with.

For instance, we write

const params = {
  Bucket: bucketName + path,
  Key: key,
  Body: buffer,
  ContentEncoding: 'base64',
  ContentType: 'image/jpeg',
  ACL: 'public-read'
};
await s3.putObject(params).promise();

to call putObject with params that has ACL set to 'public-read' to let anyone read the uploaded file.

Conclusion

To set uploaded file and folder permissions with AWS S3 Node.js SDK, we can set the ACL property of the object that we call putObject with.

Categories
JavaScript Answers

How to use a regex for route matching in Express and Node.js?

Sometimes, we want to use a regex for route matching in Express and Node.js

In this article, we’ll look at how to use a regex for route matching in Express and Node.js.

How to use a regex for route matching in Express and Node.js?

To use a regex for route matching in Express and Node.js, we can use a regular JavaScript regex as route path argument.

For instance, we write

app.get(/^\/(discussion|page)\/(.+)/, (req, res, next) => {
  //...
});

to call app.get with a regex that matches discussion or page with any characters after either one.

Conclusion

To use a regex for route matching in Express and Node.js, we can use a regular JavaScript regex as route path argument.