Categories
JavaScript Answers

How to add toast notifications with toastr and JavaScript?

Sometimes, we want to add toast notifications with toastr and JavaScript.

In this article, we’ll look at how to add toast notifications with toastr and JavaScript.

How to add toast notifications with toastr and JavaScript?

To add toast notifications with toastr and JavaScript, we can add the toastr script and CSS.

Then we can use the toastr object to create toasts.

For instance, we write:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.css" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.js"></script>

to add the toastr CSS and JavaScript file and jQuery.

jQuery is required for toastr.

Then we call toastr.success with a message string to show a success message by writing:

toastr.success('Success messages');

Conclusion

To add toast notifications with toastr and JavaScript, we can add the toastr script and CSS.

Then we can use the toastr object to create toasts.

Categories
JavaScript Answers

How to validate a ISO 8601 date using moment.js?

Sometimes, we want to validate a ISO 8601 date using moment.js.

In this article, we’ll look at how to validate a ISO 8601 date using moment.js.

How to validate a ISO 8601 date using moment.js?

To validate a ISO 8601 date using moment.js, we can use the isValid method.

For instance, we write:

const isValid = moment("2022-10-10T14:48:00", moment.ISO_8601).isValid()
console.log(isValid)

We call moment with the date string to parse and moment.ISO_8601 to parse the date string as an ISO 8601 date string.

Then we call isValid to return whether the date string is a valid date string according to the format we specified when parsing.

Therefore, isValid should be true since we the date string is a valid ISO 8601 date string.

Conclusion

To validate a ISO 8601 date using moment.js, we can use the isValid method.

Categories
JavaScript Answers

How to count the number of files in a directory using JavaScript and Node.js?

Sometimes, we want to count the number of files in a directory using JavaScript and Node.js.

In this article, we’ll look at how to count the number of files in a directory using JavaScript and Node.js.

How to count the number of files in a directory using JavaScript and Node.js?

To count the number of files in a directory using JavaScript and Node.js, we can use the fs.readdir with the irectory to get the array of files data in the directory.

Then we can use the length property to get the number of files in the directory.

For instance, we write:

const { promises: fs } = require('fs');
const dir = '/';

const getNumFiles = async (dir) => {
  const files = await fs.readdir(dir)
  console.log(files.length)
}
getNumFiles(dir)

to define the getNumFiles function that takes the dir directory string.

In the function, we call fs.readdir with dir to read the files data in dir into an array.

Then we get the number of files from the length property.

Conclusion

To count the number of files in a directory using JavaScript and Node.js, we can use the fs.readdir with the irectory to get the array of files data in the directory.

Then we can use the length property to get the number of files in the directory.

Categories
JavaScript Answers

How to capitalize first letter of each word in JavaScript?

Sometimes, we want to capitalize first letter of each word in JavaScript.

In this article, we’ll look at how to capitalize first letter of each word in JavaScript.

How to capitalize first letter of each word in JavaScript?

To capitalize first letter of each word in JavaScript, we can use the string’s replace method.

For instance, we write:

const str = 'foo bar baz'
const newStr = str.toLowerCase().replace(/^\w|\s\w/g, (letter) => {
  return letter.toUpperCase();
})
console.log(newStr)

We call replace with a regex to match the first letter of each word and a callback that replace those letters with an upper case letter.

Therefore, newStr is 'Foo Bar Baz'.

Conclusion

To capitalize first letter of each word in JavaScript, we can use the string’s replace method.

Categories
JavaScript Answers Nodejs

How to get the ID parameter from the URL with Express and Node.js?

Sometimes, we want to get the ID parameter from the URL with Express and Node.js.

In this article, we’ll look at how to get the ID parameter from the URL with Express and Node.js.

How to get the ID parameter from the URL with Express and Node.js?

To get the ID parameter from the URL with Express and Node.js, we can get it from the req.params object.

For instance, we write:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.get('/user/:id/docs', (req, res) => {
  const { id } = req.params;
  res.send(id)
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

to add the /user/:id/docs route with:

app.get('/user/:id/docs', (req, res) => {
  const { id } = req.params;
  res.send(id)
});

':id' is the placeholder for the id URL parameter.

We get the ID value from req.params.

Therefore, if we go to /user/1/docs, we see 1 returned as the response.

Conclusion

To get the ID parameter from the URL with Express and Node.js, we can get it from the req.params object.