Categories
JavaScript Answers

How to fix proxy not working for React and Node?

To fix proxy not working for React and Node, we need to clear cache.

To do this, we

  1. Delete package-lock.json and node_modules in the React project

  2. Turn off React terminal and npm install all dependencies again on the React project

  3. Turn back on React App

Categories
JavaScript Answers

How to glob for pattern excluding multiple files with Node.js?

To glob for pattern excluding multiple files with Node.js, we set the ignore property.

For instance, we write

const glob = require("glob");

glob(
  "**/*",
  { ignore: ["index.html", "js", "js/app.js", "js/lib.js"] },
  (err, files) => {
    console.log(files);
  }
);

to call glob with an object with the ignore property to ignore the patterns listed in the array.

And then we get the results from files.

Categories
JavaScript Answers

How to set Node.js global proxy setting?

To set Node.js global proxy setting, we use the global-tunnel-ng package.

For instance, we write

const globalTunnel = require("global-tunnel-ng");

globalTunnel.initialize({
  host: "10.0.0.10",
  port: 8080,
  proxyAuth: "userId:password",
  sockets: 50,
});

to call initialize with an object with the proxy options and credentials to connect to the proxy.

Categories
JavaScript Answers

How to read and output jpg image with Node?

To read and output jpg image with Node, we read it with readFile.

For instance, we write

const http = require("http");
const fs = require("fs");

fs.readFile("image.jpg", (err, data) => {
  if (err) {
    throw err;
  }
  http
    .createServer((req, res) => {
      res.writeHead(200, { "Content-Type": "image/jpeg" });
      res.end(data);
    })
    .listen(8080);
});

to call readFile to read the image.jpg file.

And then we call createServer with a callback that calls res.end with the read data to return that as the response.

We call writeHead to write the Content-Type header with the MIME type as its value.

Categories
JavaScript Answers

How to fix ISODate is not defined with Node?

To fix ISODate is not defined with Node, we use the Date constructor.

For instance, we write

const d = new Date(dateString);

to create a date from dateString with the Date constructor.