Categories
JavaScript Answers

How to use Node .npmrc with registry?

To use Node .npmrc with registry, we put the registry URLs in the .npmrc file.

To do this, we write something like

registry=https://npm-proxy.fury.io/AUTH_TOKEN/USER_NAME/

in the .npmrc file to make npm use our npm package registry URL.

Categories
JavaScript Answers

How to use React IndexRoute in react-router v4?

To use React IndexRoute in react-router v4, we add the Route component.

For instance, we write

<Router>
  <div>
    <ul>
      <li>
        <Link to="/">Home</Link>
      </li>
      <li>
        <Link to="/about">About</Link>
      </li>
      <li>
        <Link to="/contact">Contact</Link>
      </li>
    </ul>
    <hr />
    <Route exact path="/" component={Home} />
    <Route exact path="/" component={About} />
    <Route exact path="/" component={Contact} />
    <Route path="/about" component={About} />
    <Route path="/contact" component={Contact} />
  </div>
</Router>

to add the Route component to map the path prop values to the component prop.

When we go to the path, the components is rendered.

Also, we add Links for the routes.

Categories
JavaScript Answers

How to create a Node HTTP Client Request with a cookie?

To create a Node HTTP Client Request with a cookie, we call the http.request method.

For instance, we write

const options = {
  hostname: "example.com",
  path: "/somePath.php",
  method: "GET",
  headers: { Cookie: "myCookie=myvalue" },
};
let results = "";
const req = http.request(options, (res) => {
  res.on("data", (chunk) => {
    results += chunk;
  });
  res.on("end", () => {});
});

req.on("error", (e) => {});

req.end();

to call http.request with the options object.

In it, we set the Cookie header to the keys and values for the cookie.

Categories
JavaScript Answers

How to query referenced objects in Node MongoDB?

To query referenced objects in Node MongoDB, we use $unwind and $lookup.

For instance, we write

db.Foo.aggregate(
  { $unwind: "$bars" },
  {
    $lookup: {
      from: "bar",
      localField: "bars",
      foreignField: "_id",
      as: "bar",
    },
  },
  {
    $match: {
      "bar.testprop": true,
    },
  }
);

to call aggregate with $unwind to unwind the $bars entries.

And then we look up the testprop field of the bars entries that’s set to true with $match.

Categories
JavaScript Answers

How to hash passwords with Node.js?

To hash passwords with Node.js, we use the bcrypt package.

For instance, we write

import * as bcrypt from "bcrypt";

export const Encrypt = {
  cryptPassword: async (password) => {
    const salt = await bcrypt.genSalt(10);
    const hash = await bcrypt.hash(password, salt);
    return hash;
  },
  comparePassword: async (password, hashPassword) => {
    return await bcrypt.compare(password, hashPassword);
  },
};

to call genSalt to create a salt in the cryptPassword function.

Then we call hash to hash the password with the salt.

And then we call bcrypt.compare to compare the raw password with the hashedPassword with compare.