Categories
JavaScript Answers

How to add file upload with Node.js and Express?

Sometimes, we want to add file upload with Node.js and Express.

In this article, we’ll look at how to add file upload with Node.js and Express.

How to add file upload with Node.js and Express?

To add file upload with Node.js and Express, we can use the express-fileupload package.

To install it, we run

npm i express-fileupload

Then we use it by writing

const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();

app.use(fileUpload());

app.post('/upload', (req, res) => {
  if (!req.files || Object.keys(req.files).length === 0) {
    return res.status(400).send('No files were uploaded.');
  }

  const {
    sampleFile
  } = req.files;

  sampleFile.mv('/somewhere/on/your/server/filename.jpg', (err) => {
    if (err) {
      return res.status(500).send(err);
    }
    res.send('File uploaded!');
  });
});

to call app.use with the middleware we get from calling fileUpload.

Then we check if req.files is defined and has any entries in it in the POST /upload route handler.

If req.files isn’t defined or has no length, then we return a 400 response.

Otherwise, we get the file and call mv to save the file as '/somewhere/on/your/server/filename.jpg'.

The callback we pass into mv when mv is done.

Conclusion

To add file upload with Node.js and Express, we can use the express-fileupload package.

Categories
JavaScript Answers

How to call Node.js fs.writeFile as a promise?

Sometimes, we want to call Node.js s.writeFile as a promise.

In this article, we’ll look at how to call Node.js fs.writeFile as a promise.

How to call Node.js fs.writeFile as a promise?

To call Node.js fs.writeFile as a promise, we can use the fs‘s promises module.

For instance, we write

const fs = require('fs').promises;

(async () => {
  const file = await fs.readFile('filename.txt', 'utf8');
  await fs.writeFile('filename.txt', 'test');
})()

to require the promises version of fs with

const fs = require('fs').promises;

Then we call fs.readFile with path of the file to read and the string with the encoding of the file.

And then we call fs.writeFile to write to filename.txt file and 'test' as its content.

Conclusion

To call Node.js fs.writeFile as a promise, we can use the fs‘s promises module.

Categories
JavaScript Answers

How to check a function is async with JavaScript?

Sometimes, we want to check a function is async with JavaScript.

In this article, we’ll look at how to check a function is async with JavaScript.

How to check a function is async with JavaScript?

To check a function is async with JavaScript, we can check if the function’s constructor is an instance of the constructor that creates an async function.

For instance, we write

const AsyncFunction = (async () => {}).constructor;

console.log(asyncFn instanceof AsyncFunction)

to get the constructor of the async async () => {} function with the constructor property.

Then we check if asyncFn is an instance of the AsyncFunction constructor with asyncFn instanceof AsyncFunction.

Conclusion

To check a function is async with JavaScript, we can check if the function’s constructor is an instance of the constructor that creates an async function.

Categories
JavaScript Answers

How to fix Nodejs connect cannot find static error?

Sometimes, we want to fix Nodejs connect cannot find static error.

In this article, we’ll look at how to fix Nodejs connect cannot find static error.

How to fix Nodejs connect cannot find static error?

To fix Nodejs connect cannot find static error, we should install the serve-static package.

We install it by running

npm i serve-static

Then we use it by writing

const connect = require('connect')
const serveStatic = require('serve-static');

const app = connect();

app.use(serveStatic("../public"));
app.listen(5000);

to call app.use with the middleware returned by serveStatic called with the path to the static folder we want to serve.

Conclusion

To fix Nodejs connect cannot find static error, we should install the serve-static package.

Categories
JavaScript Answers

How to return certain fields with .populate() from Mongoose?

Sometimes, we want to return certain fields with .populate() from Mongoose.

In this article, we’ll look at how to return certain fields with .populate() from Mongoose.

How to return certain fields with .populate() from Mongoose?

To return certain fields with .populate() from Mongoose, we can call populate with the fields we want to populate.

For instance, we write

Model
  .findOne({
    _id
  })
  .populate('field', 'name')

to call populate with 'field' and 'name' to populate the field and name fields in the returned documents.

Conclusion

To return certain fields with .populate() from Mongoose, we can call populate with the fields we want to populate.