Categories
JavaScript Answers

How to add error handling with Express Passport in Node.js?

Sometimes, we want to add error handling with Express Passport in Node.js.

In this article, we’ll look at how to add error handling with Express Passport in Node.js.

How to add error handling with Express Passport in Node.js?

To add error handling with Express Passport in Node.js, we can set the failureRedirect and failureFlash options.

For instance, we write

app.post('/login', passport.authenticate('local', {
  successRedirect: '/loggedin',
  failureRedirect: '/login',
  failureFlash: true
}));

to set failureRedirect to '/login' to redirect to the /login route when login failed.

When failureFlash is set to true, we can store the message we call next with if the connect-flash middleware is installed.

Conclusion

To add error handling with Express Passport in Node.js, we can set the failureRedirect and failureFlash options.

Categories
JavaScript Answers

How to use populate and aggregate in same statement with MongoDB and Node.js?

Sometimes, we want to use populate and aggregate in same statement with MongoDB and Node.js.

In this article, we’ll look at how to use populate and aggregate in same statement with MongoDB and Node.js.

How to use populate and aggregate in same statement with MongoDB and Node.js?

To use populate and aggregate in same statement with MongoDB and Node.js, we can call populate with the aggregation result returned from aggergate.

For instance, we write

const pop = async () => {
  const appointments = await Appointments.aggregate([
    //...
  ]);
  await Patients.populate(appointments, {
    path: "patient"
  });
  return appointments;
}

to call aggregate to return a promise with the aggregation result.

Then we call populate with the appointments aggregation to populate the aggregation result with the 'patient' field.

And then we return the appointments result with the populate results.

Conclusion

To use populate and aggregate in same statement with MongoDB and Node.js, we can call populate with the aggregation result returned from aggergate.

Categories
JavaScript Answers

How to convert a directory structure in the filesystem to JSON with Node.js?

Sometimes, we want to convert a directory structure in the filesystem to JSON with Node.js.

In this article, we’ll look at how to convert a directory structure in the filesystem to JSON with Node.js.

How to convert a directory structure in the filesystem to JSON with Node.js?

To convert a directory structure in the filesystem to JSON with Node.js, we can loop through the files and directories recursively.

For instance, we write

const fs = require('fs')
const path = require('path')

const dirTree = (filename) => {
  const stats = fs.lstatSync(filename)
  const info = {
    path: filename,
    name: path.basename(filename)
  };

  if (stats.isDirectory()) {
    info.type = "folder";
    info.children = fs.readdirSync(filename).map((child) => {
      return dirTree(`${filename}/${child}`);
    });
  } else {
    info.type = "file";
  }
  return info;
}

to define the dirTree function that gets the items in a directory with lstatSync.

Then we check if filename is a directory with stats.isDirectory.

If it is, then we call readdirSync to read the folder and call map with a callback that calls dirTree to read the directory.

Otherwise, we set info.type to 'file'.

And finally, we return info when we’re done.

Conclusion

To convert a directory structure in the filesystem to JSON with Node.js, we can loop through the files and directories recursively.

Categories
JavaScript Answers

How to store database config in Node.js and Express app?

Sometimes, we want to store database config in Node.js and Express app.

In this article, we’ll look at how to store database config in Node.js and Express app.

How to store database config in Node.js and Express app?

To store database config in Node.js and Express app, we can use the dotenv module.

To install it, we run

npm i dotenv

Then we use it by writing

app.js

require('dotenv').config()

in our app’s entry point file.

And then in our .env file, we write

DATABASE_PASSWORD=mypw
DATABASE_NAME=some_db

Then we can use the environment variable values from process.env like

process.env.DATABASE_PASSWORD

Conclusion

To store database config in Node.js and Express app, we can use the dotenv module.

Categories
JavaScript Answers

How to get HTTP method in controller with Express.js?

Sometimes, we want to get HTTP method in controller with Express.js.

In this article, we’ll look at how to get HTTP method in controller with Express.js.

How to get HTTP method in controller with Express.js?

To get HTTP method in controller with Express.js, we can use the req.method property.

For instance, we write

const register = (req, res) => {
  if (req.method == "POST") {
    //...
  }
  res.render('user/registration.html', {
    form: form.toHTML()
  });
}

app.get('/register', register)
app.post('/register', register)

to define the register route handler function.

In it, we check the HTTP method used to make the request with req.method.

Then we can use the same route handler function for routes with different methods.

Conclusion

To get HTTP method in controller with Express.js, we can use the req.method property.