Categories
JavaScript Answers

How to listen for the mouse wheel event in modern browsers with JavaScript?

Sometimes, we want to listen for the mouse wheel event in modern browsers with JavaScript.

In this article, we’ll look at how to listen for the mouse wheel event in modern browsers with JavaScript.

How to listen for the mouse wheel event in modern browsers with JavaScript?

To listen for the mouse wheel event in modern browsers with JavaScript, we listen to the wheel event.

For instance, we write

window.addEventListener("wheel", (event) => {
  const delta = Math.sign(event.deltaY);
  console.info(delta);
});

to call addEventListener to listen to the 'wheel' event.

In the event listener, we call Math.sign with event.deltaY to get -1 if we’re scrolling down and 1 if we’re scrolling up.

Conclusion

To listen for the mouse wheel event in modern browsers with JavaScript, we listen to the wheel event.

Categories
JavaScript Answers

How to create a Node.js server that accepts POST requests with JavaScript?

Sometimes, we want to create a Node.js server that accepts POST requests with JavaScript.

In this article, we’ll look at how to create a Node.js server that accepts POST requests with JavaScript.

How to create a Node.js server that accepts POST requests with JavaScript?

To create a Node.js server that accepts POST requests with JavaScript, we check the request.method property.

For instance, we write

const http = require("http");
const server = http.createServer((request, response) => {
  response.writeHead(200, { "Content-Type": "text/plain" });
  if (request.method === "GET") {
    response.end("received GET request.");
  } else if (request.method === "POST") {
    response.end("received POST request.");
  } else {
    response.end("Undefined request .");
  }
});

server.listen(8000);
console.log("Server running on port 8000");

to call createServer with a callback that’s called when a request is received.

If request.method is 'POST', then a post request is received.

Conclusion

To create a Node.js server that accepts POST requests with JavaScript, we check the request.method property.

Categories
JavaScript Answers

How to disable F5 and browser refresh using JavaScript?

Sometimes, we want to disable F5 and browser refresh using JavaScript.

In this article, we’ll look at how to disable F5 and browser refresh using JavaScript.

How to disable F5 and browser refresh using JavaScript?

To disable F5 and browser refresh using JavaScript, we stop the default behavior when pressing F5.

For instance, we write

document.addEventListener("keydown", (e) => {
  if (e.keyCode === 116) {
    e.preventDefault();
  }
});

to call addEventListener to listen to the keydown event.

In it, we check if the keyCode property is 116, which is the code for F5.

If it’s true, then we call preventDefault to stop the default refresh behavior.

Conclusion

To disable F5 and browser refresh using JavaScript, we stop the default behavior when pressing F5.

Categories
JavaScript Answers

How to return hostname with Node.js and JavaScript?

Sometimes, we want to return hostname with Node.js and JavaScript.

In this article, we’ll look at how to return hostname with Node.js and JavaScript.

How to return hostname with Node.js and JavaScript?

To return hostname with Node.js and JavaScript, we use the os.hostname method.

For instance, we write

const os = require("os");
const hostname = os.hostname();

to call os.hostname to return a string with the hostname.

Conclusion

To return hostname with Node.js and JavaScript, we use the os.hostname method.

Categories
JavaScript Answers

How to listen all interfaces instead of localhost only with Express app server in JavaScript?

Sometimes, we want to listen all interfaces instead of localhost only with Express app server in JavaScript.

In this article, we’ll look at how to listen all interfaces instead of localhost only with Express app server in JavaScript.

How to listen all interfaces instead of localhost only with Express app server in JavaScript?

To listen all interfaces instead of localhost only with Express app server in JavaScript, we call listen without the 2nd argument.

For instance, we write

app.listen(3000);

to listen to all interfaces on port 3000.

Conclusion

To listen all interfaces instead of localhost only with Express app server in JavaScript, we call listen without the 2nd argument.