Categories
JavaScript Answers

How to let others on a local network, access my Node.js app while it’s running on a machine?

To let others on a local network, access my Node.js app while it’s running on a machine, we call listen with the IP address of the computer

For instance, we write

server.listen(3000, "192.168.0.3");

to call listen with the IP address of the computer that the server is running on to allow external connections on port 3000.

Categories
JavaScript Answers

How to encrypt data that needs to be decrypted in Node.js?

To encrypt data that needs to be decrypted in Node.js, we use the crypto module.

For instance, we write

const crypto = require("crypto");
const assert = require("assert");

const algorithm = "aes256";
const key = "password";
const text = "I love kittens";

const cipher = crypto.createCipher(algorithm, key);
const encrypted = cipher.update(text, "utf8", "hex") + cipher.final("hex");

const decipher = crypto.createDecipher(algorithm, key);
const decrypted =
  decipher.update(encrypted, "hex", "utf8") + decipher.final("utf8");

to create a cipher with createCipher.

Then we call cipher.update to encrypt the text string.

Next, we call createDecipher with the same algorithm and key we call createCipher with.

The we call decipher.update to decrypt the encrypted string.

Categories
JavaScript Answers

How to fix Uncaught TypeError: Object.values is not a function JavaScript?

To fix Uncaught TypeError: Object.values is not a function JavaScript, we can use Object.keys instead.

For instance, we write

const vals = Object.keys(countries).map((key) => {
  return countries[key];
});

to get the keys from the countries object with Object.keys.

Then we call map with a callback to get the value from each key in countries and return the array of values.

Categories
JavaScript Answers

How to fix Mongoose: CastError: Cast to ObjectId failed for value “[object Object]” at path “_id” with Node.js?

To fix Mongoose: CastError: Cast to ObjectId failed for value "[object Object]" at path "_id" with Node.js, we make sure we’re casting a valid value to an object ID.

For instance, we write

const mongoose = require("mongoose");

console.log(mongoose.Types.ObjectId.isValid("53cb6b9b4f4ddef1ad47f943"));
console.log(mongoose.Types.ObjectId.isValid("whatever"));

to use the mongoose.Types.ObjectId.isValid to check if the value we’re trying to cast to an object ID is a valid value.

The first console log should log true since it’s a valid object ID value.

And the 2nd one logs false since it’s not a valid object ID value.

Categories
JavaScript Answers

How to parse Query String in Node.js?

To parse Query String in Node.js, we use the querystring module.

For instance, we write

const http = require("http");
const queryString = require("querystring");

http
  .createServer((oRequest, oResponse) => {
    let oQueryParams;
    if (oRequest.url.indexOf("?") >= 0) {
      oQueryParams = queryString.parse(oRequest.url.replace(/^.*\?/, ""));
      console.log(oQueryParams);
    }

    oResponse.writeHead(200, { "Content-Type": "text/plain" });
    oResponse.end("Hello world.");
  })
  .listen(3000, "127.0.0.1");

to call queryString.parse to parse the request URL that we got from oRequest.url.

We get pack an object with the keys and values of the query string.