It is possible to clear a cookies only on a specified HTTP route on ExpressJS?

Viewed 14

It is possible to clear a cookies only on a specified HTTP route on ExpressJS? the case is i only want to clear some cookies on a very spesific route, because when i do this:

app.get('/', (req, res) => {
    res.clearCookie('foo');
    res.render('login', {
        layout: 'layout/main-layout.ejs',
        page_title:'Login',
     );
});

its clear those cookies for all the following route directory, for example the cookies on /dashboard route will also be cleared.

1 Answers

You can clear cookies for a specific route by doing below example.

res.clearCookie('foo', { path: '/' })

Above code clear the cookies in the root directory. If you want to remove cookies in another directory (let's take admin) you can change the directory from "/" to "/admin" and the code will clear the cookies in the /admin directory.

You can read about this more at Express Docs

If you found this answer helpful mark this answer as the correct answer.

Related