Categories
JavaScript Answers

How to determine whether 64 bit or 32 bit Node executable installed?

Sometimes, we want to determine whether 64 bit or 32 bit Node executable installed.

In this article, we’ll look at how to determine whether 64 bit or 32 bit Node executable installed.

How to determine whether 64 bit or 32 bit Node executable installed?

To determine whether 64 bit or 32 bit Node executable installed, we use the process.arch property.

For instance, we run

node -p "process.arch"

to print the value of the process.arch property into the console.

The possible values for process.arch includes

'arm', 'arm64' 'ia32' 'mips' 'mipsel' 'ppc' 'ppc64' 's390' 's390x' 'x32' 'x64'

Conclusion

To determine whether 64 bit or 32 bit Node executable installed, we use the process.arch property.

Categories
JavaScript Answers

How to fix the ‘Cannot find module’ when importing components with absolute paths with Jest?

To fix the ‘Cannot find module’ when importing components with absolute paths with Jest, we can set the roots setting of the Jest config.

For instance, we write

"jest": {
  "roots": [
    "<rootDir>",
    "/home/some/path/"
  ],
  "modulePaths": [
    "<rootDir>",
    "/home/some/other/path"
  ],
  "moduleDirectories": [
    "node_modules"
  ],
}

to add the '<rootDir>' and "/home/some/path/" path strings into the roots array.

This makes these 2 paths the root path when we use absolute imports.

Likewise, we add the same entries to modulePaths to add them as root paths for absolute imports.

Categories
JavaScript Answers

How to find user by username LIKE value with Mongoose.js?

Sometimes, we want to find user by username LIKE value with Mongoose.js.

In this article, we’ll look at how to find user by username LIKE value with Mongoose.js.

How to find user by username LIKE value with Mongoose.js?

To find user by username LIKE value with Mongoose.js, we can use findOne with a regex.

For instance, we write

const name = 'Peter';

model.findOne({
  name: new RegExp(`^${name}$`, "i")
}, (err, doc) => {
  //...
});

to call model.findOne with an object that searches the name field with a regex.

We set name to a regex that matches anything that has name in the string.

And we add the 'i' flag to the regex to search in a case-insensitive manner.

As a result, any entry with name having 'Peter' anywhere in the string would be matched.

Conclusion

To find user by username LIKE value with Mongoose.js, we can use findOne with a regex.

Categories
JavaScript Answers

How to pass additional form fields to the local authentication strategy Using Node.js Passport.js?

Sometimes, we want to pass additional form fields to the local authentication strategy Using Node.js Passport.js.

In this article, we’ll look at how to pass additional form fields to the local authentication strategy Using Node.js Passport.js.

How to pass additional form fields to the local authentication strategy Using Node.js Passport.js?

To pass additional form fields to the local authentication strategy Using Node.js Passport.js, we set the passReqToCallback option to true.

For instance, we write

passport.use(new LocalStrategy({
    usernameField: 'email',
    passReqToCallback: true
  },
  (req, email, password, done) =>{
    // ...
  }
));

to create a LocalStrategy instance with an object that has passReqToCallback set to true.

As a result, the middleware function in the 2nd argument would have the full req object from other middlewares before it passed into it.

Conclusion

To pass additional form fields to the local authentication strategy Using Node.js Passport.js, we set the passReqToCallback option to true.

Categories
JavaScript Answers

How to return data from Axios API with Node.js?

Sometimes, we want to return data from Axios API with Node.js

In this article, we’ll look at how to return data from Axios API with Node.js.

How to return data from Axios API with Node.js?

To return data from Axios API with Node.js, we can return the properties from the data property.

For instance, we write

const axiosTest = async (url) => {
  try {
    const {
      data: response
    } = await axios.get(url)
    return response
  } catch (error) {
    console.log(error);
  }
}

to call axios.get with the url string.

It returns a promise and we get the resolve value of the promise with await.

And then we get the data property to get the response body object and we return it after assigning to response.

We use a catch block to catch any errors.

Conclusion

To return data from Axios API with Node.js, we can return the properties from the data property.