Categories
JavaScript Answers

How to create a sleep or delay in Node.js?

Sometimes, we want to create a sleep or delay in Node.js.

In this article, we’ll look at how to create a sleep or delay in Node.js.

How to create a sleep or delay in Node.js?

To create a sleep or delay in Node.js, we can create a function that returns a promise to pause execution for a specific amount of time.

For instance, we write

const sleep = (millis) => {
  return new Promise(resolve => setTimeout(resolve, millis));
}


const test = async () => {
  await sleep(1000)
  console.log("one second has elapsed")
}

to define the sleep function that returns a promise which calls setTimeout with resolve as a callback after millis milliseconds.

Then we call sleep in test with 1000 to pause the function for 1000 milliseconds and then call console.log to log "one second has elapsed".

Conclusion

To create a sleep or delay in Node.js, we can create a function that returns a promise to pause execution for a specific amount of time.

Categories
JavaScript Answers

How to use Sequelize findOne to find the latest entry?

Sometimes, we want to use Sequelize findOne to find the latest entry.

In this article, we’ll look at how to use Sequelize findOne to find the latest entry.

How to use Sequelize findOne to find the latest entry?

To use Sequelize findOne to find the latest entry, we can use the findOne method with the order property to order items by createdAt descending.

For instance, we write

model.findOne({
  where: {
    key
  },
  order: [
    ['createdAt', 'DESC']
  ],
});

to call findOne with an object with the order property that’s set to an array with ['createdAt', 'DESC'] to order the results by the createdAt value sorted in descending order.

And we use where to return the result with key set to key.

Conclusion

To use Sequelize findOne to find the latest entry, we can use the findOne method with the order property to order items by createdAt descending.

Categories
JavaScript Answers

How to throttle the number of API requests per second in Node.js?

Sometimes, we want to throttle the number of API requests per second in Node.js.

In this article, we’ll look at how to throttle the number of API requests per second in Node.js.

How to throttle the number of API requests per second in Node.js?

To throttle the number of API requests per second in Node.js, we can use the simple-rate-limiter package.

We install it by running

npm i simple-rate-limiter

Then we use it by writing

const limit = require("simple-rate-limiter");
const request = limit(require("request")).to(10).per(1000);

We call limit with the request module.

And we limit the number of requests to 10 per second with .to(10).per(1000).

Conclusion

To throttle the number of API requests per second in Node.js, we can use the simple-rate-limiter package.

Categories
JavaScript Answers

How to get the path from the request with Node.js?

Sometimes, we want to get the path from the request with Node.js.

In this article, we’ll look at how to get the path from the request with Node.js.

How to get the path from the request with Node.js?

To get the path from the request with Node.js, we can use the req.url property.

For instance, we write

const http = require('http');
const url = require('url');

http
  .createServer((req, res) => {
    res.send(req.url)
  })
  .listen(8080, '0.0.0.0');

to call createServer with a callback that gets the request path from req.url.

And we call res.send to send that has the response.

Conclusion

To get the path from the request with Node.js, we can use the req.url property.

Categories
JavaScript Answers

How to test an Express app with Mocha?

Sometimes, we want to test an Express app with Mocha.

In this article, we’ll look at how to test an Express app with Mocha.

How to test an Express app with Mocha?

To test an Express app with Mocha, we can make requests in our tests to the endpoint we’re testing with supertest.

For instance, we write

const request = require('supertest')
const {
  app
} = require('./express-app')
const assert = require("assert");

describe('POST /', () => {
  it('should fail', (done) => {
    request(app)
      .post('/')
      .send({
        'imgUri': 'foobar'
      })
      .expect(500)
      .end((err, res) => {
        done();
      })
  })
});

to call it with a callback with the test code.

In it, we call request with app to run the app.

And then we call post to make a POST request.

send sends the POST request body.

We then call expect to test the returned response code.

And then we call done in the end callback to finish the test.

Conclusion

To test an Express app with Mocha, we can make requests in our tests to the endpoint we’re testing with supertest.