Categories
JavaScript Answers

How to authenticate Supertest requests with Passport?

Sometimes, we want to authenticate Supertest requests with Passport.

In this article, we’ll look at how to authenticate Supertest requests with Passport.

How to authenticate Supertest requests with Passport?

To authenticate Supertest requests with Passport, we can make a request to log in.

For instance, we write

const request = require('superagent');

const user1 = request.agent();
user1
  .post('http://localhost:4000/signin')
  .send({
    user: 'abc@example.com',
    password: 'password'
  })
  .end((err, res) => {
    // ...
  });

to call post with the login endpoint URL.

And we call send with a JSON object with the user and password parameters accepted by the login endpoint.

Finally, we call end with a callback to get the response from res.

Conclusion

To authenticate Supertest requests with Passport, we can make a request to log in.

Categories
JavaScript Answers

How to set object ID as a data type in Node.js Mongoose?

Sometimes, we want to set object ID as a data type in Node.js Mongoose.

In this article, we’ll look at how to set object ID as a data type in Node.js Mongoose.

How to set object ID as a data type in Node.js Mongoose?

To set object ID as a data type in Node.js Mongoose, we can use Schema.ObjectId as the type.

For instance, we write

const mongoose = require('mongoose');
const {
  Schema
} = mongoose;
const {
  ObjectId
} = Schema;

const SchemaProduct = new Schema({
  categoryId: ObjectId,
  title: String,
  price: Number
});

to use the Schema constructor to create a new schema.

And we set the categoryId field to the ObjectId type by setting categoryId to ObjectId.

Conclusion

To set object ID as a data type in Node.js Mongoose, we can use Schema.ObjectId as the type.

Categories
JavaScript Answers

How to check Node.js Mongoose connection state without creating a new connection?

Sometimes, we want to check Node.js Mongoose connection state without creating new connection.

In this article, we’ll look at how to check Node.js Mongoose connection state without creating new connection.

How to check Node.js Mongoose connection state without creating a new connection?

To check Node.js Mongoose connection state without creating new connection, we can use the mongoose.connection.readyState property.

For instance, we write

const mongoose = require('mongoose');

console.log(mongoose.connection.readyState);

The possible values for mongoose.connection.readyState include:

  • 0: disconnected
  • 1: connected
  • 2: connecting
  • 3: disconnecting

Conclusion

To check Node.js Mongoose connection state without creating new connection, we can use the mongoose.connection.readyState property.

Categories
JavaScript Answers

How to make multiple route parameters optional in Node.js Express?

Sometimes, we want to make multiple route parameters optional in Node.js Express.

In this article, we’ll look at how to make multiple route parameters optional in Node.js Express.

How to make multiple route parameters optional in Node.js Express?

To make multiple route parameters optional in Node.js Express, we can put a question mark after each optional route parameter placeholder.

For instance, we write

app.get('/articles/:year?/:month?/:day?', (req, res) => {
  const {
    year,
    month,
    day
  } = req.params
  //...
})

to call .app.get with a route path that has the optional year, month, and day parameter as indicated by the ? after each parameter.

Then we get the year, month, and day parameter value from req.params.

Conclusion

To make multiple route parameters optional in Node.js Express, we can put a question mark after each optional route parameter placeholder.

Categories
JavaScript Answers

How to format console output with Node.js?

Sometimes, we want to format console output with Node.js.

In this article, we’ll look at how to format console output with Node.js.

How to format console output with Node.js?

To format console output with Node.js, we can use the string padStart and padEnd methods.

For instance, we write

'abc'.padStart(10, "foo");

to prepend the 'abc' string with 'foo' until its length is 10.

So it returns "foofoofabc".

If we skip the 2nd argument, empty space will be prepended.

Also, we can append to a string until it reaches the given length with padEnd.

For instance, we write

'abc'.padEnd(10, "foo");

to call padEnd to append the 'abc' string with 'foo' until its length is 10.

So it returns "abcfoofoof".

If we skip the 2nd argument, empty space will be appended.

Conclusion

To format console output with Node.js, we can use the string padStart and padEnd methods.