Categories
JavaScript Answers

How to erase characters printed in console with Node.js?

Sometimes, we want to erase characters printed in console with Node.js.

In this article, we’ll look at how to erase characters printed in console with Node.js.

How to erase characters printed in console with Node.js?

To erase characters printed in console with Node.js, we can use the process.stdout.write method with a carriage return.

For instance, we write

process.stdout.write("\r");

to to move the cursor pack to the start of the line.

Then we can call process.stdout.write again to write something else to the same line.

Conclusion

To erase characters printed in console with Node.js, we can use the process.stdout.write method with a carriage return.

Categories
JavaScript Answers

How to make a Node.js app not exit on error?

Sometimes, we want to make a Node.js app not exit on error.

In this article, we’ll look at how to make a Node.js app not exit on error.

How to make a Node.js app not exit on error?

To make a Node.js app not exit on error, we can add a listener for the uncaughtException with process.on.

For instance, we write

process.on('uncaughtException', (err) => {
  console.log('Caught exception: ', err);
});

to call process.on with 'uncaughtException' and a callback that runs when the uncaughtException event is emitted.

The uncaughtException event is triggered when there’s an unhandled error occurring in the app.

In the callback, we get the error object from err.

Conclusion

To make a Node.js app not exit on error, we can add a listener for the uncaughtException with process.on.

Categories
JavaScript Answers

How to use async and await with Node.js AWS SDK?

Sometimes, we want to use async and await with Node.js AWS SDK.

In this article, we’ll look at how to use async and await with Node.js AWS SDK.

How to use async and await with Node.js AWS SDK?

To use async and await with Node.js AWS SDK, we can call the promise method after we call the AWS SDK method.

For instance, we write

try {
  const key = await kms.generateDataKey().promise();
} catch (e) {
  console.log(e);
}

to call promise after call generateDataKey to return a promise with the resolved value assigned to key.

Conclusion

To use async and await with Node.js AWS SDK, we can call the promise method after we call the AWS SDK method.

Categories
JavaScript Answers

How to retrieve the last inserted ID with Node.js MySQL?

Sometimes, we want to retrieve the last inserted ID with Node.js MySQL.

In this article, we’ll look at how to retrieve the last inserted ID with Node.js MySQL.

How to retrieve the last inserted ID with Node.js MySQL?

To retrieve the last inserted ID with Node.js MySQL, we can get it from the callback that’s called when the query is done.

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 connection.query to make a INSERT query.

We call query with a callback and get the ID of the inserted entry with result.insertId.

Conclusion

To retrieve the last inserted ID with Node.js MySQL, we can get it from the callback that’s called when the query is done.

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.