Categories
JavaScript Answers

How to check if an array contains any element of another array in JavaScript?

To check if an array contains any element of another array in JavaScript, we use the some method.

For instance, we write

const arr1 = ["apple", "grape"];
const arr2 = ["apple", "banana", "pineapple"];
const found = arr1.some((r) => arr2.includes(r));

to call arr1.some with a callback that checks if the item being iterated through r is in arr1 is in arr2 with arr2.includes.

If the callback returns true, then some returns true.

Categories
JavaScript Answers

How to generate HTML with Node.js?

To generate HTML with Node.js, we call the createServer method.

For instance, we write

const http = require("http");

http
  .createServer((req, res) => {
    res.write("<html><head></head><body>");
    res.write("<p>Write your HTML content here</p>");
    res.end("</body></html>");
  })
  .listen(1337);

to call createServer with a callback that calls res.write to write HTML as the response body.

We call res.end to finish writing the response body.

Categories
JavaScript Answers

How to send HTML file to client with Node?

To send HTML file to client with Node, we call the sendFile method.

For instance, we write

const app = express();

app.get("/test", (req, res) => {
  res.sendFile("views/test.html", { root: __dirname });
});

to call res.sendFile with the template’s path to render the template.

root is the root folder for the template files.

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.