Categories
JavaScript Answers

How to scroll down until you can’t anymore with Puppeteer?

Sometimes, we want to scroll down until you can’t anymore with Puppeteer.

In this article, we’ll look at how to scroll down until you can’t anymore with Puppeteer.

How to scroll down until you can’t anymore with Puppeteer?

To scroll down until you can’t anymore with Puppeteer, we can scroll periodically down the page until the height scrolled is bigger than or equal to the scroll height of the scroll container.

For instance, we write

const puppeteer = require('puppeteer');

const autoScroll = async (page) => {
  await page.evaluate(async () => {
    await new Promise((resolve, reject) => {
      let totalHeight = 0;
      const distance = 100;
      const timer = setInterval(() => {
        const scrollHeight = document.body.scrollHeight;
        window.scrollBy(0, distance);
        totalHeight += distance;

        if (totalHeight >= scrollHeight) {
          clearInterval(timer);
          resolve();
        }
      }, 100);
    });
  });
}

const openPageAndScroll = async () => {
  const browser = await puppeteer.launch({
    headless: false
  });
  const page = await browser.newPage();
  await page.goto('https://www.yoursite.com');
  await page.setViewport({
    width: 1200,
    height: 800
  });
  await autoScroll(page);
  //...
  await browser.close();
}

openPageAndScroll()

to define the autoScroll function.

In it, we call setInterval with a callback that calls window.scrollBy to scroll by distance pixels down.

We then add distance to totalHeight.

And then if totalHeight >= scrollHeight, we call clearInterval to clear the timer and call resolve to resolve the promise.

And we scroll every 100ms.

Next, we define the openPageAndScroll function that calls autoScroll and use await to wait for the promise to resolve before running the next line.

Conclusion

To scroll down until you can’t anymore with Puppeteer, we can scroll periodically down the page until the height scrolled is bigger than or equal to the scroll height of the scroll container.

Categories
JavaScript Answers

How to implement login authentication in Node.js?

Sometimes, we want to implement login authentication in Node.js.

In this article, we’ll look at how to implement login authentication in Node.js.

How to implement login authentication in Node.js?

To implement login authentication in Node.js, we can add our own middleware to check the session object before calling the route handler.

For instance, we write

const checkAuth = (req, res, next) => {
  if (!req.session.userId) {
    res.send('You are not authorized');
  } else {
    next();
  }
}

app.get('/my_secret_page', checkAuth, (req, res) => {
  res.send('You are logged in');
});

app.post('/login', (req, res) => {
  const post = req.body;
  if (post.user === 'john' && post.password === 'johnspassword') {
    req.session.userId = johnsUserId;
    res.redirect('/my_secret_page');
  } else {
    res.send('Bad user/pass');
  }
})

app.get('/logout', (req, res) => {
  delete req.session.userId;
  res.redirect('/login');
});

to define the checkAuth middleware function that checks if req.session.userId is present.

We set req.session.userId in the /login route when login is successful.

If req.session.userId isn’t set, we call res.send with a 'You are not authorized'.

Otherwise, we call next to call the route middleware.

Next, we add the endpoints with app.get and app.post.

We add checkAuth as an argument in /my_secret_page to run checkAuth to check for presence of userId before running the route.

In the /login route handler, we check the user and password from the req.body request body object.

And if they both match, we call res.redirect to redirect to /my_secret_page since login is successful.

In /logout, we delete req.session.userId to remove the current user info and call res.redirect to redirect to /login.

Conclusion

To implement login authentication in Node.js, we can add our own middleware to check the session object before calling the route handler.

Categories
JavaScript Answers

How to read a stream into a buffer with Node.js?

Sometimes, we want to read a stream into a buffer with Node.js.

In this article, we’ll look at how to read a stream into a buffer with Node.js.

How to read a stream into a buffer with Node.js?

To read a stream into a buffer with Node.js, we can use the on method to listen for the data event.

For instance, we write

const bufs = [];
stdout.on('data', (d) => {
  bufs.push(d);
});
stdout.on('end', () => {
  const buf = Buffer.concat(bufs);
})

to call stdout.on with 'data' to listen for the data event.

In the callback, we call bufs.push with d to append d into the bufs array.

And then we call on with 'end' to listen to the end event which is triggered when the stream is finished.

In the callback, we call Buffer.concat with bufs to combine the buffer chunks into a single buffer object.

Conclusion

To read a stream into a buffer with Node.js, we can use the on method to listen for the data event.

Categories
JavaScript Answers

How to convert a MongoDB object ID to string with Node.js?

Sometimes, we want to convert a MongoDB object ID to string with Node.js.

In this article, we’ll look at how to convert a MongoDB object ID to string with Node.js.

How to convert a MongoDB object ID to string with Node.js?

To convert a MongoDB object ID to string with Node.js, we can use the object ID toString method.

For instance, we write

user._id.toString()

to convert the _id value to a string.

Conclusion

To convert a MongoDB object ID to string with Node.js, we can use the object ID toString method.

Categories
JavaScript Answers

How to implement user permissions with Node.js and Express.js?

Sometimes, we want to implement user permissions with Node.js and Express.js

in this article, we’ll look at how to implement user permissions with Node.js and Express.js.

How to implement user permissions with Node.js and Express.js?

To implement user permissions with Node.js and Express.js, we can define a function that returns a middleware function that checks the role of the user and acts accordingly.

For instance, we write

const requireRole = (role) => {
  return (req, res, next) => {
    if (req.session.user && req.session.user.role === role) {
      next();
    } else {
      res.send(403);
    }
  }
}

app.get("/foo", foo.index);
app.get("/foo/:id", requireRole("user"), foo.show);
app.post("/foo", requireRole("admin"), foo.create);

app.all("/foo/bar", requireRole("admin"));
app.all("/foo/bar/*", requireRole("user"));

to define the requireRole function that returns a middleware function that checks the role of the user with req.session.user.role === role.

And if the user has the role, then we call next to call the route middleware.

Otherwise, we return a 403 response.

Then we use requireRole by calling it with the role name before the route handler method in app.get, app.post, and app.all.

Conclusion

To implement user permissions with Node.js and Express.js, we can define a function that returns a middleware function that checks the role of the user and acts accordingly.