Categories
JavaScript Answers

How to get an accurate timestamp in Node.js?

Sometimes, we want to get an accurate timestamp in Node.js.

In this article, we’ll look at how to get an accurate timestamp in Node.js.

How to get an accurate timestamp in Node.js?

To get an accurate timestamp in Node.js, we can use the process.hrtime.bigint method.

For instance, we write

process.hrtime.bigint()

to call process.hrtime.bigint to return the current timestamp in nanoseconds as a JavaScript bigint.

Conclusion

To get an accurate timestamp in Node.js, we can use the process.hrtime.bigint method.

Categories
JavaScript Answers

How to drop a database with Mongoose?

Sometimes, we want to drop a database with Mongoose.

In this article, we’ll look at how to drop a database with Mongoose.

How to drop a database with Mongoose?

To drop a database with Mongoose, we can remove all the content of a collection.

For instance, we write

Model.remove({}, (err) => {
  console.log('collection removed')
});

to call Model.remove with an empty object to remove all items in the collection that’s mapped to Model.

The callback is run when all the items are removed from the collection.

Conclusion

To drop a database with Mongoose, we can remove all the content of a collection.

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.