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.

Categories
JavaScript Answers

How to fix the ‘Error: Cannot GET /’ error message with Node.js and Express?

To fix the ‘Error: Cannot GET /’ error message with Node.js and Express, we need to add the route handler for the GET / route.

For instance, we write

app.get("/", (req, res) => {
  res.render("index.html");
});

to call app.get with '/' and the route handler to handle GET requests to the / route.

In the route handler for the GET / route, we call res.render to render the index.html template on the screen.

Categories
Vue Answers

How to filter on array with Vue.js?

Sometimes, we want to filter on array with Vue.js.

In this article, we’ll look at how to filter on array with Vue.js.

How to filter on array with Vue.js?

To filter on array with Vue.js, we can return the filtered array in a computed property.

For instance, we write

<template>
  <div>
    <div>
      <input type="text" v-model="search" />
      <label>Search Users:</label>
    </div>
    <ul>
      <li v-for="user in filteredAndSorted" :key="user.id">{{ user.name }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  //...
  data() {
    return {
      search: "",
      userList: [
        {
          id: 1,
          name: "Prem",
        },
        {
          id: 1,
          name: "Chandu",
        },
        {
          id: 1,
          name: "Shravya",
        },
      ],
    };
  },
  computed: {
    filteredAndSorted() {
      const compare = (a, b) => {
        if (a.name < b.name) return -1;
        if (a.name > b.name) return 1;
        return 0;
      };

      return this.userList
        .filter((user) => {
          return user.name.toLowerCase().includes(this.search.toLowerCase());
        })
        .sort(compare);
    },
  },
  //...
};
</script>

to create the filteredAndSorted to return the userList array filtered and sorted.

We filter the values by the this.search value that we get from typing in the input.

v-model updates the this.search value with the latest input value.

Then we use v-for to render all the items in the filteredAndSorted array.

Conclusion

To filter on array with Vue.js, we can return the filtered array in a computed property.

Categories
Vue Answers

How to get data as object with Vue.js?

Sometimes, we want to get data as object with Vue.js.

In this article, we’ll look at how to get data as object with Vue.js.

How to get data as object with Vue.js?

To get data as object with Vue.js, we can use the this.$data property.

For instance, we write

<script>
export default {
  //...
  methods: {
    log() {
      console.log(this.$data.a);
      console.log(this.$data.b);
      console.log(this.$data.foo);
    },
  },
  //...
};
</script>

to get the a reactive property with this.$data.a.

Likewise, we get the b reactive property with this.$data.b and the foo reactive property with this.$data.foo.

Conclusion

To get data as object with Vue.js, we can use the this.$data property.