Express JS - Cookies Handling

Task 1: Define and Handle /cookie1

Create a route /cookie1 that sets two cookies, token and last_visit_timestamp, in the user's browser. When the browser sends these cookies back to the server, display their values using the cookie1.ejs template. Additionally, calculate and display the time (in seconds) since the user's last visit by subtracting the last_visit_timestamp cookie value from the current time. Ensure that the last_visit_timestamp cookie is updated with the current timestamp on each request. Refresh the page at random intervals to test its functionality.


Task 2: Add Cookie Form at /add-cookie

Develop a route /add-cookie with a form containing two input fields: Cookie Name and Cookie Value. When the form is submitted, store the specified cookie in the user's browser and redirect them to /home. Include a link to the "Add Cookie" form on the homepage for easy navigation.


Task 3: Remove Cookie Form at /remove-cookie

Implement a route /remove-cookie to serve a form that allows users to enter a cookie name for deletion. Upon form submission, a POST handler on the same URL should remove the specified cookie from the user's browser and redirect them back to /home. Add a link to the "Remove Cookie" page on the homepage.


Task 4: AJAX-Based Cookie Removal at /remove-cookie-ajax

Set up a route /remove-cookie-ajax with a form for AJAX-based cookie removal. The form should take the cookie name as input. When submitted, send an AJAX request to a dedicated server-side handler that removes the specified cookie. Update the server to process the AJAX request and include any necessary data, such as the cookie name to be removed. Add a link to "Remove Cookie using AJAX" on the homepage.


Task 5: Display All Cookies at /show-cookies

Create a route /show-cookies to display all cookies sent by the browser in a tabular format. Add a link to the "Show Cookies" page on the homepage for quick access.

Comments