Categories
JavaScript Answers

How to bind Express.js to a specific IP address?

Sometimes, we want to bind Express.js to a specific IP address.

In this article, we’ll look at how to bind Express.js to a specific IP address.

How to bind Express.js to a specific IP address?

To bind Express.js to a specific IP address, we call listen with the IP address as the 2nd argument.

For instance, we write

const server = app.listen(3000, "127.0.0.1", onServerListening);

to call app.listen with the port, IP address string, and the onServerListening callback function that runs when the server is started.

This will bind the Express app to 127.0.0.1

Conclusion

To bind Express.js to a specific IP address, we call listen with the IP address as the 2nd argument.

Categories
JavaScript Answers

How to set request header when making connection with socket.io client?

Sometimes, we want to set request header when making connection with socket.io client.

In this article, we’ll look at how to set request header when making connection with socket.io client.

How to set request header when making connection with socket.io client?

To set request header when making connection with socket.io client, we can add the extraHeaders option.

For instance, we write

const socket = io("http://localhost", {
  extraHeaders: {
    Authorization: "...",
  },
});

to call io with the socket.io server URL and an object that has the extraHeaders option set to an object with the request headers to send.

We send the Authorization header set to the token as the value.

Conclusion

To set request header when making connection with socket.io client, we can add the extraHeaders option.

Categories
JavaScript Answers

How to send error HTTP response in Express and Node.js?

Sometimes, we want to send error HTTP response in Express and Node.js.

In this article, we’ll look at how to send error HTTP response in Express and Node.js.

How to send error HTTP response in Express and Node.js?

To send error HTTP response in Express and Node.js, we can call the status and send methods.

For instance, we write

response.status(code).send(new Error("description"));

to call status with the status code number and call send with the Error instance with the error text to return a error response in our route handler function.

Conclusion

To send error HTTP response in Express and Node.js, we can call the status and send methods.

Categories
JavaScript Answers

How to tell ESLint to prefer single quotes around strings?

Sometimes, we want to tell ESLint to prefer single quotes around strings.

In this article, we’ll look at how to tell ESLint to prefer single quotes around strings.

How to tell ESLint to prefer single quotes around strings?

To tell ESLint to prefer single quotes around strings, we add the singleQuote option.

For instance, we write

{
  //...
  "rules": {
    "prettier/prettier": [
      "warn",
      {
        "singleQuote": true,
        "semi": true
      }
    ]
  }
  //...
}

to set the singleQuote option to true in .eslintrc`.

Then ESLint will send a warning if single quote strings aren’t being used.

Conclusion

To tell ESLint to prefer single quotes around strings, we add the singleQuote option.

Categories
JavaScript Answers

How to handle FormData with Node.js Express.js 4?

Sometimes, we want to handle FormData with Node.js Express.js 4.

In this article, we’ll look at how to handle FormData with Node.js Express.js 4.

How to handle FormData with Node.js Express.js 4?

To handle FormData with Node.js Express.js 4, we can use multer.

To install it, we run

npm i multer

Then we use it by writing

const multer = require("multer");
const upload = multer();

app.post("/send", upload.none(), (req, res) => {
  const formData = req.body;
  console.log("form data", formData);
  res.sendStatus(200);
});

to call multer to return the upload object.

Then we use the upload.none method to return a middleware that doesn’t take file uploads but parses form data requests.

Next, we get the form data requests from req.body in the route handler.

Conclusion

To handle FormData with Node.js Express.js 4, we can use multer.