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.

Categories
JavaScript Answers

How to throttle and queue up API requests due to per second cap with Node.js?

Sometimes, we want to throttle and queue up API requests due to per second cap with Node.js.

In this article, we’ll look at how to throttle and queue up API requests due to per second cap with Node.js.

How to throttle and queue up API requests due to per second cap with Node.js?

To throttle and queue up API requests due to per second cap with Node.js, we can use the node-rate-limiter package.

To install it, we run

npm i limiter

Then we use it by writing

const { RateLimiter } = require("limiter");

const limiter = new RateLimiter({ tokensPerInterval: 150, interval: "hour" });
const throttledRequest = async (...args) => {
  const remainingRequests = await limiter.removeTokens(1);
  callMyMessageSendingFunction(...args);
};

to create a RateLimiter instance with { tokensPerInterval: 150, interval: "hour" } to allow 150 requests her hour.

Then we create the throttledRequest function that calls limiter.removeTokens with 1 and a callback that calls callMyMessageSendingFunction to return a function that throttles the request calls to 150 her hour.

Conclusion

To throttle and queue up API requests due to per second cap with Node.js, we can use the node-rate-limiter package.