Categories
JavaScript Answers

How to get error status code with Node.js http get with JavaScript?

Sometimes, we want to get error status code with Node.js http get with JavaScript.

In this article, we’ll look at how to get error status code with Node.js http get with JavaScript.

How to get error status code with Node.js http get with JavaScript?

To get error status code with Node.js http get with JavaScript, we use the statusCode property.

For instance, we write

const https = require("https");

https
  .get("https://example.com/", (res) => {
    console.log("statusCode: ", res.statusCode);
    console.log("headers: ", res.headers);

    res.on("data", (d) => {
      process.stdout.write(d);
    });
  })
  .on("error", (e) => {
    console.error(e);
  });

to to make a request to example.com.

Then we get the status code of the request with res.statusCode.

Conclusion

To get error status code with Node.js http get with JavaScript, we use the statusCode property.

Categories
JavaScript Answers

How to read uploaded text file contents in HTML with JavaScript?

Sometimes, we want to read uploaded text file contents in HTML with JavaScript.

In this article, we’ll look at how to read uploaded text file contents in HTML with JavaScript.

How to read uploaded text file contents in HTML with JavaScript?

To read uploaded text file contents in HTML with JavaScript, we use file reader.

For instance, we write

const reader = new FileReader();
reader.onload = (event) => console.log(event.target.result);
reader.onerror = (error) => console.error(error);
reader.readAsText(file);

to create a FileReader object.

And then we set the onload and onerror event handlers.

We get the read file from event.target.result.

Then we call readAsText to read the file into a string.

Conclusion

To read uploaded text file contents in HTML with JavaScript, we use file reader.

Categories
JavaScript Answers

How to stop reloading page with Enter Key with JavaScript?

Sometimes, we want to stop reloading page with Enter Key with JavaScript.

In this article, we’ll look at how to stop reloading page with Enter Key with JavaScript.

How to stop reloading page with Enter Key with JavaScript?

To stop reloading page with Enter Key with JavaScript, we call preventDefault.

For instance, we write

document.getElementById("searchForm").addEventListener(
  "submit",
  (e) => {
    e.preventDefault();
    search(document.getElementById("searchText"));
  },
  false
);

to select the form with getElementById.

Then we call addEventListener to add an event listener for the submit event.

We use a function that calls preventDefault to stop the default submit behavior to stop the page reloading.

And then we call search with the field with the search text we get from getElementById.

Conclusion

To stop reloading page with Enter Key with JavaScript, we call preventDefault.

Categories
React Answers

How to get current date with React.js?

Sometimes, we want to get current date with React.js.

In this article, we’ll look at how to get current date with React.js.

How to get current date with React.js?

To get current date with React.js, we use the date’s toLocaleString method.

For instance, we write

const d = new Date().toLocaleString();

to create a date object and then call toLocaleString on it to return a string with the current date and time in the current locale.

Conclusion

To get current date with React.js, we use the date’s toLocaleString method.

Categories
JavaScript Answers

How to fix TypeError: Cannot read property ‘style’ of null error with JavaScript?

Sometimes, we want to fix TypeError: Cannot read property ‘style’ of null error with JavaScript.

In this article, we’ll look at how to fix TypeError: Cannot read property ‘style’ of null error with JavaScript.

How to fix TypeError: Cannot read property ‘style’ of null error with JavaScript?

To fix TypeError: Cannot read property ‘style’ of null error with JavaScript, we should make sure the element we’re setting the styles for exists.

For instance, we write

document.getElementById("note").style.display = "block";

to style the display style for the element with ID note.

We should make sure getElementById doesn’t return null.

Conclusion

To fix TypeError: Cannot read property ‘style’ of null error with JavaScript, we should make sure the element we’re setting the styles for exists.