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.

Categories
JavaScript Answers

How to add a response timeout with Express.js and Node.js?

Sometimes, we want to add a response timeout with Express.js and Node.js.

In this article, we’ll look at how to add a response timeout with Express.js and Node.js.

How to add a response timeout with Express.js and Node.js?

To add a response timeout with Express.js and Node.js, we can use the connect-timeout module.

To install it, we run

npm i connect-timeout

Then we use it by writing

const timeout = require('connect-timeout');

const haltOnTimedout = (req, res, next) => {
  if (!req.timedout) {
    next();
  }
}
app.use(timeout(120000));
app.use(haltOnTimedout);

to define the haltOnTimeout function.

We check if the request has timed out with req.timedout.

If it’s false, then we call next to call the next middleware.

And we add the middleware returned by timeout called with the request timeout in milliseconds and the halfOnTimedout function with app.use in the order they’re listed to lets us set a timeout for all requests to 120000 milliseconds.

Conclusion

To add a response timeout with Express.js and Node.js, we can use the connect-timeout module.

Categories
JavaScript Answers

How to return a complex JSON response with Node.js and Express?

Sometimes, we want to return a complex JSON response with Node.js and Express.

In this article, we’ll look at how to return a complex JSON response with Node.js and Express.

How to return a complex JSON response with Node.js and Express?

To return a complex JSON response with Node.js and Express, we can use the res.json method.

For instance, we write

res.json({
  fileName
})

to call res.json with the JSON object that we want to return as the response.

As a result, the client would get a JSON string with the stringified JSON object as the response.

Conclusion

To return a complex JSON response with Node.js and Express, we can use the res.json method.