Categories
JavaScript Answers

How to fix Node npm install doesn’t create node_modules directory?

To fix Node npm install doesn’t create node_modules directory, we create a package.json file.

To do this, we run

npm init

to create a package.json file in the project folder.

Categories
JavaScript Answers

How to fix Node.js https pem error: routines:PEM_read_bio:no start line?

To fix Node.js https pem error: routines:PEM_read_bio:no start line,. we read the SSL key and certificate file.

For instance, we write

const options = {
  key: fs.readFileSync("./key.pem", "utf8"),
  cert: fs.readFileSync("./server.crt", "utf8"),
};

to read key and cert files with readFileSync.

key is the key file.

And cert is the certificate file.

Categories
JavaScript Answers

How to fix Passport.js in Node not removing session on logout?

To fix Passport.js in Node not removing session on logout, we call the session.destroy method.

For instance, we write

app.get("/logout", (req, res) => {
  req.session.destroy((err) => {
    res.redirect("/");
  });
});

to call req.session.destroy to destroy the session.

We call it with a callback that’s called when the session is destroyed.

And we call redirect in the callback to redirect to /.

Categories
JavaScript Answers

How to retrieve last inserted id with Node MySQL?

To retrieve last inserted id with Node MySQL, we use the insertedId property.

For instance, we write

connection.query(
  "INSERT INTO posts SET ?",
  { title: "test" },
  (err, result, fields) => {
    if (err) throw err;

    console.log(result.insertId);
  }
);

to call query with a callback that gets the result.insertId property to get the ID of the last inserted item.

Categories
JavaScript Answers

How to fix Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE during PhoneGap installation with Node?

To fix Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE during PhoneGap installation with Node, we disable the strict-ssl option.

To do this, we run

npm config set strict-ssl false

to disable the strict-ssl option to make npm accept untrusted SSL certificates.

Then we run

npm config set strict-ssl true

after installation to turn the certificate check back on.