Categories
JavaScript Answers

How to send a https request to a rest service in Node?

To send a https request to a rest service in Node, we use the https.request method.

For instance, we write

const https = require("https");
const options = {
  hostname: "www.example.com",
  port: 443,
  path: "/",
  method: "GET",
  headers: {
    Accept: "plain/html",
    "Accept-Encoding": "*",
  },
};

const req = https.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`);
  console.log("headers:", res.headers);

  res.on("data", (d) => {
    process.stdout.write(d);
  });
});

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

req.end();

to call https.request with the options object to make a request to https://www.example.com.

The object has the headers, the request method, the port and the path.

We get the response from res in the callback.

We get the status code with res.statusCode.

We get the response headers with res.headers.

And we get the response body from the data event callback’s d parameter.

We get errors from the error parameter from the error event handler.

Categories
JavaScript Answers

How to execute PowerShell script from Node.js?

To execute PowerShell script from Node.js, we call the spawn function.

For instance, we write

const spawn = require("child_process").spawn;
const child = spawn("powershell.exe", ["c:\\temp\\helloworld.ps1"]);
child.stdout.on("data", (data) => {
  console.log(data);
});
child.stderr.on("data", (data) => {
  console.log(data);
});
child.on("exit", function () {
  console.log("Powershell Script finished");
});
child.stdin.end();

to call spawn to run powershell.exe with the path to the PowerShell script.

We listen for output by calling child.stdout.on to listen for data events.

We listen for errors by calling child.stderr.on to listen for data events.

And we call child.on with 'exit' to listen for process exit.

Categories
JavaScript Answers

How to add a debug script to Node NPM?

To add a debug script to Node NPM, we run node with the --inspect option.

For instance, we write

{
  "scripts": {
    "debug": "node --inspect server.js"
  }
}

to add the debug script into our package.json file.

We make it run node with the --inspect option to let us debug server.js.

Then we run it with

npm run debug
Categories
JavaScript Answers

How to check if ID exists in a collection with Node Mongoose?

To check if ID exists in a collection with Node Mongoose, we call the countDocument method.

For instance, we write

User.countDocuments({ _id: userID }, (err, count) => {
  if (count > 0) {
    //...;
  }
});

to call countDocuments with an object with _id set to the value we’re looking for.

The count parameter in the callback has the number of items with the _id value.

Categories
JavaScript Answers

How to send no cache headers in Node.js server?

To send no cache headers in Node.js server, we call the writeHead method.

For instance, we write

res.writeHead(200, {
  "Content-Type": mimeType,
  "Content-Length": contents.length,
  "Accept-Ranges": "bytes",
  "Cache-Control": "no-cache",
});

to call writHeader with an object with the response headers and values.

We return the Content-Type, Content-Length, Accept-Ranges, and Cache-Control headers.

We set Cache-Control to no-cache to disable cache.