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 in next HTTP request e.g. /display-cookies, 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
Create a route /add-cookie
that should display 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 Add Cookie in header that shall point to /add-cookie endpoint 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. On form submission, a POST handler on the same URL should remove the specified cookie from the user's browser and redirect user /home
. Add "Remove Cookie" link in header to make the navigation easy.
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 handler /remove-cookie-ajax that should remove the specified cookie. It shall send appropriate respones to browser, that shall be displayed to user. Add a link to "Remove Cookie using AJAX" on the homepage. And add HomePage link in header.
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
Post a Comment