Categories
JavaScript Answers

How to delete cookie in Express and JavaScript?

Spread the love

Sometimes, we want to delete cookie in Express and JavaScript.

In this article, we’ll look at how to delete cookie in Express and JavaScript.

How to delete cookie in Express and JavaScript?

To delete cookie in Express and JavaScript, we can use the res.clearCookie method.

For instance, we write:

const express = require('express')
const app = express()
const port = 3000

app.get('/logout', (req, res) => {
  res.clearCookie('cookie');
  return res.sendStatus(200);
})


app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

to call res.clearCookie with the key of the cookie to delete in the /logout endpoint.

Conclusion

To delete cookie in Express and JavaScript, we can use the res.clearCookie method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *