Categories
JavaScript Answers

How to go back 1 folder level with __dirname in Node.js?

Sometimes, we want to go back 1 folder level with __dirname in Node.js.

In this article, we’ll look at how to go back 1 folder level with __dirname in Node.js.

How to go back 1 folder level with __dirname in Node.js?

To go back 1 folder level with __dirname in Node.js, we can use the path.join method.

For instance, we write

const reqPath = path.join(__dirname, "../../../");

to call path.join with __dirname and "../../../" to return the path of the folder that’s 3 levels above __dirname and assign it to reqPath.

Conclusion

To go back 1 folder level with __dirname in Node.js, we can use the path.join method.

Categories
JavaScript Answers

How to make synchronous requests in Node.js?

Sometimes, we want to make synchronous requests in Node.js.

In this article, we’ll look at how to make synchronous requests in Node.js.

How to make synchronous requests in Node.js?

To make synchronous requests in Node.js, we can use the sync-request package.

To install it, we run

npm i sync-request

Then we use it by writing

const request = require("sync-request");

try {
  const res = request("GET", url);
  const uComp = res.split("\n")[1].split(", ")[1];
  doSomething(uComp);
} catch (e) {}

to call request with 'GET' and url to make a GET request to the url.

Then we get the response with res.

Conclusion

To make synchronous requests in Node.js, we can use the sync-request package.

Categories
JavaScript Answers

How to get HTTP headers with Node.js?

Sometimes, we want to get HTTP headers with Node.js.

In this article, we’ll look at how to get HTTP headers with Node.js.

How to get HTTP headers with Node.js?

To get HTTP headers with Node.js, we can use the res.headers properties.

For instance, we write

const http = require("http");
const options = {
  method: "HEAD",
  host: "example.com",
  port: 80,
  path: "/",
};
const req = http.request(options, (res) => {
  console.log(JSON.stringify(res.headers));
});
req.end();

to call http.request to make a request.

We call it with the request options object and a callback that runs when the response is received.

Then we get the response headers for the request with res.headers.

Conclusion

To get HTTP headers with Node.js, we can use the res.headers properties.

Categories
JavaScript Answers

How to get number of processors available with Node.js?

Sometimes, we want to get number of processors available with Node.js.

In this article, we’ll look at how to get number of processors available with Node.js.

How to get number of processors available with Node.js?

To get number of processors available with Node.js, we can use the os.cpus method.

For instance, we write

const os = require("os");
const cpuCount = os.cpus().length;

to get the cpuCount with os.cpus().length.

Conclusion

To get number of processors available with Node.js, we can use the os.cpus method.

Categories
JavaScript Answers

How to receive email in Node.js?

Sometimes, we want to receive email in Node.js.

In this article, we’ll look at how to receive email in Node.js.

How to receive email in Node.js?

To receive email in Node.js, we can use the node-imap package.

To install it, we run

npm install imap

Then we use it by writing

const Imap = require("imap");
const { inspect } = require("util");

const imap = new Imap({
  user: "mygmailname@gmail.com",
  password: "mygmailpassword",
  host: "imap.gmail.com",
  port: 993,
  tls: true,
});

const openInbox = (cb) => {
  imap.openBox("INBOX", true, cb);
};

imap.once("ready", () => {
  openInbox(function (err, box) {
    if (err) throw err;
    const f = imap.seq.fetch("1:3", {
      bodies: "HEADER.FIELDS (FROM TO SUBJECT DATE)",
      struct: true,
    });
    f.on("message", (msg, seqno) => {
      console.log("Message #%d", seqno);
      const prefix = "(#" + seqno + ") ";
      msg.on("body", (stream, info) => {
        let buffer = "";
        stream.on("data", (chunk) => {
          buffer += chunk.toString("utf8");
        });
        stream.once("end", () => {
          console.log(
            prefix + "Parsed header: %s",
            inspect(Imap.parseHeader(buffer))
          );
        });
      });
      msg.once("attributes", (attrs) => {
        console.log(prefix + "Attributes: %s", inspect(attrs, false, 8));
      });
      msg.once("end", () => {
        console.log(prefix + "Finished");
      });
    });
    f.once("error", (err) => {
      console.log("Fetch error: " + err);
    });
    f.once("end", () => {
      console.log("Done fetching all messages!");
      imap.end();
    });
  });
});

imap.once("error", (err) => {
  console.log(err);
});

imap.once("end", () => {
  console.log("Connection ended");
});

imap.connect();

to receive email via IMAP.

We create an IMAP instance with the credentials for the email server.

Then we listen to the ready event with imap.onace.

In it, we call imap.seq.fetch to fetch the emails.

And then we call f.on with 'message' and the callback that gets the messages from a stream.

We received everything when the end event is received, which we listen to by calling msg.once with 'end' and the event handler callback.

And we call imap.connect() to make the connection to the IMAP server.

Conclusion

To receive email in Node.js, we can use the node-imap package.