Categories
JavaScript Answers

How to check if user is logged in with Passport.js?

Sometimes, we want to check if user is logged in with Passport.js.

In this article, we’ll look at how to check if user is logged in with Passport.js.

How to check if user is logged in with Passport.js?

To check if user is logged in with Passport.js, we can check is req.user is defined.

For instance, we write

if (req.user) {
  // logged in
} else {
  // not logged in
}

to do the check in a middleware function.

We write

const loggedIn = (req, res, next) => {
  if (req.user) {
    next();
  } else {
    res.redirect('/login');
  }
}

app.get('/me', loggedIn, (req, res, next) => {
  //...
});

to define the loggedIn middleware function with the if statement.

We call next to run the next middleware function if req.user is set and call res.redirect to redirect to the /login route otherwise.

And then we can use loggedIn in a route by passing it in as an argument.

Conclusion

To check if user is logged in with Passport.js, we can check is req.user is defined.

Categories
JavaScript Answers

How to append to new line in Node.js?

Sometimes, we want to append to new line in Node.js.

In this article, we’ll look at how to append to new line in Node.js.

How to append to new line in Node.js?

To append to new line in Node.js, we can use the write method with the os.EOL constant.

For instance, we write

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

fs.open('./log.txt', 'a', 666, (e, id) => {
  fs.write(id, text + os.EOL, null, 'utf8', () => {
    fs.close(id, () => {
      console.log('file is updated');
    });
  });
});

to call fs.open to open the ./log.txt file with 666 file permission and 'a' for append.

Then we call fs.write with the text appended with the os.EOL character, which is the new line character for the platform the app is running on.

Then we call fs.close to close the file handle given by id.

Categories
JavaScript Answers

How to get folder path from a file with Node.js?

Sometimes, we want to get folder path from a file with Node.js.

In this article, we’ll look at how to get folder path from a file with Node.js.

How to get folder path from a file with Node.js?

To get folder path from a file with Node.js, we can use the path.dirname method.

For instance, we write

const path = require('path');
const folder = path.dirname('G:\\node-demos\\7-node-module\\demo\\config.json')

to call path.dirname with the path string we want to get the folder path for.

It returns the folder path string which we assign to folder.

Conclusion

To get folder path from a file with Node.js, we can use the path.dirname method.

Categories
JavaScript Answers

How to detect CTRL+C key press in Node.js?

Sometimes, we want to detect CTRL+C key press in Node.js.

In this article, we’ll look at how to detect CTRL+C key press in Node.js.

How to detect CTRL+C key press in Node.js?

To detect CTRL+C key press in Node.js, we can call process.on with 'SIGINT'.

For instance, we write

process.on('SIGINT', () => {
  console.log("Caught interrupt signal");

  if (shouldExit) {
    process.exit();
  }
});

to call process.on with 'SIGINT' to listen for ctrl+c key press.

The interrupt signal is triggered by ctrl+c.

In the callback, we call process.exit to exit the app if shouldExit is true.

Conclusion

To detect CTRL+C key press in Node.js, we can call process.on with 'SIGINT'.

Categories
JavaScript Answers

How to get full list of users with Mongoose?

Sometimes, we want to get full list of users with Mongoose.

In this article, we’ll look at how to get full list of users with Mongoose.

How to get full list of users with Mongoose?

To get full list of users with Mongoose, we can use the User.find method.

For instance, we write

User.find({}, (err, users) => {
  console.log(users)
});

to call User.find with an empty object to retrieve the full list of users.

The full users list is stored in the users parameter as an array.

Conclusion

To get full list of users with Mongoose, we can use the User.find method.