Categories
JavaScript Answers

How to upload images using Node.js, Express, and Mongoose?

Sometimes, we want to upload images using Node.js, Express, and Mongoose.

In this article, we’ll look at how to upload images using Node.js, Express, and Mongoose.

How to upload images using Node.js, Express, and Mongoose?

To upload images using Node.js, Express, and Mongoose, we canm use the bodyParser middleware.

To use it, we write

app.use(express.bodyParser());

app.post('/todo/create', (req, res) => {
  res.send(console.dir(req.files));
});

to call express.bodyParser to return a middleware to parse multipart form data request bodies.

And then we call app.use to use the middleware.

Then we create a POST route with app.post.

And we get the uploaded files from req.files in the route handler.

Then in the template, we write

form(action="/todo/create", method="POST", enctype="multipart/form-data")
    input(type='file', name='todo')
    button(type='submit') New

to add a form element with the action attribute set to /todo/create to submit our form data to the route we just created.

We set enctype to multipart/form-data to make it submit form data.

In the form element, we add a file input to let us accept files.

Conclusion

To upload images using Node.js, Express, and Mongoose, we canm use the bodyParser middleware.

Categories
JavaScript Answers

How to make requests in sequential order Node.js?

Sometimes, we want to make requests in sequential order Node.js.

In this article, we’ll look at how to make requests in sequential order Node.js.

How to make requests in sequential order Node.js?

To make requests in sequential order Node.js, we can use axios.

To install it, we run

npm i axios

Then we write

const axios = require('axios')

const makeRequests = async () => {
  const {
    data
  } = await axios.get(url1)
  const {
    data: data2
  } = await axios.get(url2)
  const {
    data: data3
  } = await axios.get(url3)
}

to call axios.get to make GET requests to 3 different URLs url1, url2, and url3.

And we get the response from the data property of the resolved value of each promise.

await returns the resolve value of the promise.

Conclusion

To make requests in sequential order Node.js, we can use axios.

Categories
JavaScript Answers

How to delete array element in document and save with Mongoose?

Sometimes, we want to delete array element in document and save with Mongoose.

In this article, we’ll look at how to delete array element in document and save with Mongoose.

How to delete array element in document and save with Mongoose?

To delete array element in document and save with Mongoose, we can use the updateOne method with the $pullAll operator.

For instance, we write

Favorite.updateOne({
  name
}, {
  $pullAll: {
    favorites: req.params.deleteUid,
  },
});

to call updateOne with an object to search for the document with the name value set to name.

The 2nd object has the $pullAll property which lets us remove the favorites entries in the selected Favorite document with the _id set to req.params.deleteUid.

Conclusion

To delete array element in document and save with Mongoose, we can use the updateOne method with the $pullAll operator.

Categories
JavaScript Answers

How to determine whether 64 bit or 32 bit Node executable installed?

Sometimes, we want to determine whether 64 bit or 32 bit Node executable installed.

In this article, we’ll look at how to determine whether 64 bit or 32 bit Node executable installed.

How to determine whether 64 bit or 32 bit Node executable installed?

To determine whether 64 bit or 32 bit Node executable installed, we use the process.arch property.

For instance, we run

node -p "process.arch"

to print the value of the process.arch property into the console.

The possible values for process.arch includes

'arm', 'arm64' 'ia32' 'mips' 'mipsel' 'ppc' 'ppc64' 's390' 's390x' 'x32' 'x64'

Conclusion

To determine whether 64 bit or 32 bit Node executable installed, we use the process.arch property.

Categories
JavaScript Answers

How to fix the ‘Cannot find module’ when importing components with absolute paths with Jest?

To fix the ‘Cannot find module’ when importing components with absolute paths with Jest, we can set the roots setting of the Jest config.

For instance, we write

"jest": {
  "roots": [
    "<rootDir>",
    "/home/some/path/"
  ],
  "modulePaths": [
    "<rootDir>",
    "/home/some/other/path"
  ],
  "moduleDirectories": [
    "node_modules"
  ],
}

to add the '<rootDir>' and "/home/some/path/" path strings into the roots array.

This makes these 2 paths the root path when we use absolute imports.

Likewise, we add the same entries to modulePaths to add them as root paths for absolute imports.