Categories
JavaScript Answers

How to set JWT token expiry time to maximum in Node?

To set JWT token expiry time to maximum in Node, we call the sign method.

For instance, we write

const token = jwt.sign({ email_id: "123@gmail.com" }, "secret", {});

to call jwt.sign to create a token with the object as the data and the 'secret' secret.

We leave out the expiry time value to set the expiry time to the maximum value.

Categories
JavaScript Answers

How to save array of strings with Node Mongoose?

To save array of strings with Node Mongoose, we put the data type in an array.

For instance, we write

const personSchema = new mongoose.Schema({
  tags: [
    {
      type: String,
    },
  ],
});

to add the tags field to the personSchema and make it a string array field with

[
  {
    type: String,
  },
];
Categories
JavaScript Answers

How to set max viewport in Node Puppeteer?

To set max viewport in Node Puppeteer, we set the --window-size option.

For instance, we write

const browser = await puppeteer.launch({
  headless: false,
  args: ["--window-size=1920,1080"],
  defaultViewport: null,
});

to call launch to launch the browser.

We set args to ["--window-size=1920,1080"] to make the window size 1920×1080.

Categories
JavaScript Answers

How to use ES6 modules in Node 12?

To use ES6 modules in Node 12, we set the --experimental-modules option.

For instance, we write

{
  "scripts": {
    "start": "node --experimental-modules src/index.mjs "
  }
}

in package.json to add the start script.

We use it to run node with the --experimental-modules on to enable ES6 modules support.

Categories
JavaScript Answers

How to fix Uncaught Error: Cannot find module ‘jquery’ with Node Electron app?

To fix Uncaught Error: Cannot find module ‘jquery’ with Node Electron app, we need to add the jQuery script into our HTML file.

For instance, we write

<!DOCTYPE html>
<html>
  <head></head>

  <body>
    <h1>Hello World!</h1>
  </body>

  <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

  <script>
    window.jQuery = window.$ = jQuery;

    $(document).ready(function () {
      console.log("jQuery is loaded");
    });
  </script>
</html>

to add the jQuery script with

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>