Categories
JavaScript Answers

How to quit a Node.js app gracefully?

Sometimes, we want to quit a Node.js app gracefully.

In this article, we’ll look at how to quit a Node.js app gracefully.

How to quit a Node.js app gracefully?

To quit a Node.js app gracefully, we can listen for the SIGINT event and call process.exit when it’s triggered.

For instance, we write

process.on('SIGINT', () => {
  process.exit();
})

to listen to the SIGINT event by calling process.on with 'SIGINT'.

In the event handler callback, we call process.exit without an an exit code to exit the app gracefully.

Conclusion

To quit a Node.js app gracefully, we can listen for the SIGINT event and call process.exit when it’s triggered.

Categories
JavaScript Answers

How to wait until an element is visible with Puppeteer?

Sometimes, we want to wait until an element is visible with Puppeteer.

In this article, we’ll look at how to wait until an element is visible with Puppeteer.

How to wait until an element is visible with Puppeteer?

To wait until an element is visible with Puppeteer, we call the page.waitForSelector method.

For instance, we write

const puppeteer = require('puppeteer');

const wait = async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage();
  await page
    .waitForSelector('#myId')
  console.log('got it')
  browser.close();
}

to call puppeteer.launch to launch Puppeteer.

Then we call browser.newPage to open the page.

And then we call page.waitForSelector with the CSS selector of the element we want to wait for.

Then once we’re done, we call browser.close to close the browser.

Conclusion

To wait until an element is visible with Puppeteer, we call the page.waitForSelector method.

Categories
JavaScript Answers

How to get a file’s last modified date in Node.js?

Sometimes, we want to get a file’s last modified date in Node.js

In this article, we’ll look at how to get a file’s last modified date in Node.js.

How to get a file’s last modified date in Node.js?

To get a file’s last modified date in Node.js, we can use the fs.stat or fs.statSync method.

For instance, we write

fs.stat("/dir/file.txt", (err, stats) => {
  const {
    mtime
  } = stats;
  console.log(mtime);
});

to call fs.stat with the path of the file that we want to get the last modified date for asynchronously.

And we get the last modified date of the file with the mtime property from the stats object in the callback.

We can do the same thing synchronously with statSync:

const stats = fs.statSync("/dir/file.txt");
const {
  mtime
} = stats;
console.log(mtime);

We call statSync with the path of the file and the stats are returned synchronously.

Conclusion

To get a file’s last modified date in Node.js, we can use the fs.stat or fs.statSync method.

Categories
JavaScript Answers

How to use an include with attributes with Node.js Sequelize?

Sometimes, we want to use an include with attributes with Node.js Sequelize.

In this article, we’ll look at how to use an include with attributes with Node.js Sequelize.

How to use an include with attributes with Node.js Sequelize?

To use an include with attributes with Node.js Sequelize, we can set the include.attributes property.

For instance, we write

Payment.findAll({
  where: {
    id
  },
  attributes: {
    exclude: ['createdAt', 'updatedAt']
  },
  include: {
    model: Customer,
    attributes: ['customerName', 'phoneNumber']
  }
})

to call findAll with an object that has the include property.

We set include.model to Customer to include Customer entries associated with Payment.

And we return the customerName and phoneNumber columns from the associated Customer.

Conclusion

To use an include with attributes with Node.js Sequelize, we can set the include.attributes property.

Categories
JavaScript Answers

How to accept form data with Express.js?

Sometimes, we want to accept form data with Express.js.

In this article, we’ll look at how to accept form data with Express.js.

How to accept form data with Express.js?

To accept form data with Express.js, we can use the express-formidable package.

To install it, we run

npm install express-formidable

Then we use it by writing

const express = require('express');
const formidable = require('express-formidable');

const app = express();

app.use(formidable());

app.post('/upload', (req, res) => {
  res.send(JSON.stringify(req.fields));
});

to call formiable to return a middleware that parses form data request data.

Then we call app.use with the returned middleware to use it.

Next, in the route handler callback, we get the form data request data from req.fields.

Conclusion

To accept form data with Express.js, we can use the express-formidable package.