Categories
JavaScript Answers

How to find a Node.js app running and kill it?

To find a Node.js app running and kill it, we run the ps and kill commands.

To find the node process number, we run

$ ps -e | grep node

with ps.

We find the node process with grep.

And then we run

$ kill -9 132

to run kill to kill the process with ID 132 where 132 is returned by ps -e | grep node.

We replace this with the actual ID of the process.

Categories
JavaScript Answers

How to wait for a stream to finish piping with Node?

To wait for a stream to finish piping with Node, we use the stream/promises module.

For instance, we write

import * as StreamPromises from "stream/promises";

//...
await StreamPromises.pipeline(sourceStream, destinationStream);

to call StreamPromises.pipeline to pipe sourceStream to destinationStream.

We use await to wait for the promise returned to finish before proceeding.

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.