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.

Categories
JavaScript Answers

How to check if a file exists with Node.js?

Sometimes, we want to check if a file exists with Node.js.

In this article, we’ll look at how to check if a file exists with Node.js.

How to check if a file exists with Node.js?

To check if a file exists with Node.js, we can use the fs.promises.access method.

For instance, we write

const checkFileExists = async (file) => {
  try {
    await fs.promises.access(file, fs.constants.F_OK)
    return true
  } catch (e) {
    return false
  }
}

to define the checkFileExists method.

In it, we call fs.promises.access with the file path string and fs.constants.F_OK to check if we can access the file at the file path.

If it’s successful, we return true.

Otherwise, we return false as we did in the catch block

Conclusion

To check if a file exists with Node.js, we can use the fs.promises.access method.

Categories
JavaScript Answers

How to programmatically send a 404 response with Express and Node.js?

Sometimes, we want to programmatically send a 404 response with Express and Node.js.

In this article, we’ll look at how to programmatically send a 404 response with Express and Node.js.

How to programmatically send a 404 response with Express and Node.js?

To programmatically send a 404 response with Express and Node.js, we can call res.sendStatus.

For instance, we write

res.sendStatus(404);

to send a response with the 404 status code.

Conclusion

To programmatically send a 404 response with Express and Node.js, we can call res.sendStatus.