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.

Categories
JavaScript Answers

How to fix ‘Error: unable to verify the first certificate’ in Node.js?

Sometimes, we want to fix ‘Error: unable to verify the first certificate’ in Node.js.

In this article, we’ll look at how to fix ‘Error: unable to verify the first certificate’ in Node.js.

How to fix ‘Error: unable to verify the first certificate’ in Node.js?

To fix ‘Error: unable to verify the first certificate’ in Node.js, we should set the appropriate root certificate.

For instance, we write

require('https').globalAgent.options.ca = require('ssl-root-cas/latest').create();

to call require with 'ssl-root-cas/latest' to require the root certificate.

Then we can set that has the value of the require('https').globalAgent.options.ca property so https would use the required root certificate.

Conclusion

To fix ‘Error: unable to verify the first certificate’ in Node.js, we should set the appropriate root certificate.

Categories
JavaScript Answers

How to add automatic HTTPS connection or redirect with Node.js and Express?

Sometimes, we want to add automatic HTTPS connection or redirect with Node.js and Express.

In this article, we’ll look at how to add automatic HTTPS connection or redirect with Node.js and Express.

How to add automatic HTTPS connection or redirect with Node.js and Express?

To add automatic HTTPS connection or redirect with Node.js and Express, we can call res.redirect.

For instance, we write

const http = express();

http.get('*', (req, res) => {
  res.redirect('https://' + req.headers.host + req.url);

})

http.listen(8080);

to call res.redirect with the URL we want to redirect to.

We get the hostname with req.headers.host.

And we get the rest of the URL from req.url.

Conclusion

To add automatic HTTPS connection or redirect with Node.js and Express, we can call res.redirect.