InterviewSolution
| 1. |
How to use cookies in Express.js? |
|
Answer» Cookies are an important PART of any backend project. They are used for various tasks like saving user INFORMATION, FORM, data, and others. We will learn how to implement cookies in an Express.js project. First create a project DIRECTORY and go to it. After that initialize a node project by GIVING the command npm init -y from the terminal. After that we will install express and cookie-parser in the project. The cookie-parser helps us to manage the requests made by the user to the server. Next, create a file server.js and inside it, put the below code. We are importing the express and cookie parser first. After that we will use express through the app and through the app using the cookie. Next, in an app.get in the root directory, we are using res.cookie() to send cookie data from the server to the client browser. Next, we are just sending a message to the client browser, that the cookie was saved successfully. Now, in the browser go to http://localhost:3000/ and we will see the message Cookie Saved Successfully. After that open the developer tools and Go to Storage and then expand Cookies and we will see our cookie been saved. |
|