Categories
JavaScript Answers

How to create folder or use existing with Node.js?

Sometimes, we want to create folder or use existing with Node.js.

In this article, we’ll look at how to create folder or use existing with Node.js.

How to create folder or use existing with Node.js?

To create folder or use existing with Node.js, we can use the recursive option with mkdirSync or mkdir.

For instance, we write

fs.mkdirSync(dirpath, {
  recursive: true
})

to create the folder at the path given by dirpath if it doesn’t exists by setting recursive to true with mkdirSync.

mkdirSync creates the folder synchronously.

We can create a folder asynchronously with mkdir by writing

await fs.promises.mkdir(dirpath, {
  recursive: true
})

Conclusion

To create folder or use existing with Node.js, we can use the recursive option with mkdirSync or mkdir.

Categories
JavaScript Answers

How to save a base64-encoded image to disk with Node.js?

Sometimes, we want to save a base64-encoded image to disk with Node.js.

In this article, we’ll look at how to save a base64-encoded image to disk with Node.js.

How to save a base64-encoded image to disk with Node.js?

To save a base64-encoded image to disk with Node.js, we can use the fs.writeFile method.

For instance, we write:

const base64Data = str.replace(/^data:image\/png;base64,/, "");

const fs = require("fs")

fs.writeFile("out.png", base64Data, 'base64', (err) => {
  console.log(err);
});

to remove the MIME type prefix from the base64 string with string replace.

Then we call fs.writeFile with the path to write the file to, the base64data string, the 'base64' encoding, and a callback that runs when writeFile is done.

If there’s an error, err will be set.

We need to pass in 'base64' to write the file as a base64 encoded file.

Conclusion

To save a base64-encoded image to disk with Node.js, we can use the fs.writeFile method.

Categories
JavaScript Answers

How to read file with async and await in Node.js?

Sometimes, we want to read file with async and await in Node.js.

In this article, we’ll look at how to read file with async and await in Node.js.

How to read file with async and await in Node.js?

To read file with async and await in Node.js, we can use the promise version of fs.readFile.

For instance, we write

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

const read = async () => {
  const data = await fs.readFile("monolitic.txt", "binary");
  return new Buffer(data);
}

to define the read function that calls fs.readFile with fs returned from the promises property of the fs module.

This version of readFile will return a promise.

It takes the file path and the encoding of the file.

And we get the file data from the promise’s resolve value.

Therefore, data has the file data.

Conclusion

To read file with async and await in Node.js, we can use the promise version of fs.readFile.

Categories
JavaScript Answers

How to specify HTTP error code using Express.js?

Sometimes, we want to specify HTTP error code using Express.js.

In this article, we’ll look at how to specify HTTP error code using Express.js.

How to specify HTTP error code using Express.js?

To specify HTTP error code using Express.js, we can use the res.status method.

For instance, we write

res.status(400);
res.send('error');

to call res.status with 400 to return a 400 response.

We also call res.send with the error message string to return an error message in the response body.

Conclusion

To specify HTTP error code using Express.js, we can use the res.status method.

Categories
JavaScript Answers

How to set cookie in Node.js using the Express framework?

Sometimes, we want to set cookie in Node.js using the Express framework.

In this article, we’ll look at how to set cookie in Node.js using the Express framework.

How to set cookie in Node.js using the Express framework?

To set cookie in Node.js using the Express framework, we can use the res.cookie method.

For instance, we write

app.use(express.cookieParser());

app.use((req, res, next) => {
  const cookie = req.cookies.cookieName;
  if (!cookie) {
    const randomNumber = Math.random().toString();
    res.cookie('cookieName', randomNumber, {
      maxAge: 900000,
      httpOnly: true
    });
    console.log('cookie created successfully');
  } else {
    console.log('cookie exists', cookie);
  }
  next();
});

to call express.cookieParser to return the cookie parser middleware.

Then we call app.use to use it.

Then we call app.use again with a callback that checks if the cookie with key cookieName exists.

If it does, then we call res.cookie with the cookie key and value, and an object with the maxAge of the cookie in seconds to return the cookie with the response.

Then we call next so the next middleware can be called.

Conclusion

To set cookie in Node.js using the Express framework, we can use the res.cookie method.