Categories
JavaScript Answers

How to generate unique ID with Node.js?

Sometimes, we want to generate unique ID with Node.js.

In this article, we’ll look at how to generate unique ID with Node.js.

How to generate unique ID with Node.js?

To generate unique ID with Node.js, we can use the uuid package.

For instance, we run

npm install uuid

Then we use it by writing

const {
  v1: uuidv1,
  v4: uuidv4,
} = require('uuid');

uuidv1();
uuidv4()

to call uuidv1 to return v1 UUID strings and call uuidv4 to return v4 UUID strings.

Conclusion

To generate unique ID with Node.js, we can use the uuid package.

Categories
JavaScript Answers

How to search an array of objects in MongoDB?

Sometimes, we want to search an array of objects in MongoDB.

In this article, we’ll look at how to search an array of objects in MongoDB.

How to search an array of objects in MongoDB?

To search an array of objects in MongoDB, we can use the $elemMatch operator.

For instance, we write

db.users.find({
  awards: {
    $elemMatch: {
      award: 'Medal',
      year: 1975
    }
  }
})

to search the awards array in the users collection for users with an awards entry that has the award property set to 'Medal' and year set to 1975.

Conclusion

To search an array of objects in MongoDB, we can use the $elemMatch operator.

Categories
JavaScript Answers

How to push items into Mongo array via Mongoose?

Sometimes, we want to push items into Mongo array via Mongoose.

In this article, we’ll look at how to push items into Mongo array via Mongoose.

How to push items into Mongo array via Mongoose?

To push items into Mongo array via Mongoose, we can use the array push method.

For instance, we write

person.friends.push(friend);
person.save(done);

to call push on person.friends to append a new entry to the person‘s friends collection.

And then we call person.save to save the person entry.

Conclusion

To push items into Mongo array via Mongoose, we can use the array push method.

Categories
JavaScript Answers

How to get file extension with Node.js?

Sometimes, we want to get file extension with Node.js.

In this article, we’ll look at how to get file extension with Node.js.

How to get file extension with Node.js?

To get file extension with Node.js, we can use the path.extname method.

For instance, we write

const path = require('path')

const ext = path.extname('index.html')

to call path.extname with 'index.html' to return the extension of index.html.

Conclusion

To get file extension with Node.js, we can use the path.extname method.

Categories
JavaScript Answers

How to add user authentication for Node.js?

Sometimes, we want to add user authentication for Node.js.

In this article, we’ll look at how to add user authentication for Node.js.

How to add user authentication for Node.js?

To add user authentication for Node.js, we can use Passport.

For instance, we write

passport.use(new LocalStrategy(
  (username, password, done) => {
    User.findOne({
      username,
      password
    }, (err, user) => {
      done(err, user);
    });
  }
));

app.post('/login',
  passport.authenticate('local', {
    failureRedirect: '/login'
  }),
  (req, res) => {
    res.redirect('/');
  });

to add Passport to the Express app we have.

We call passport.use with a LocalStrategy instance to let us authenticate users by looking them up from our own MongoDB database.

We use User.findOne to find the user.

Then we add the /login route that uses the middleware returned by passport.authenticate for authentication before we redirect to /.

Conclusion

To add user authentication for Node.js, we can use Passport.