Skip to content

Commit

Permalink
How are Cookies used and What are Sessions in ExpressJS & 2526 56837 …
Browse files Browse the repository at this point in the history
…7652626
  • Loading branch information
omarbelkady authored Jun 12, 2021
1 parent ada1704 commit bd1bc63
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Cookies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Cookies

- Data sent to the client side from the server and stored on the client side
- To use Cookies in ExpressJS we use a library called cookie-parser
- To create a cookie I must set a route for it

```js
var ckpars = require("cookie-parser");
var app = require("express");
app.get("/", function(request, response){
response.cookie("name","express").send("cookie-set");
});

app.listen(3000);

```


### How To Add Cookies with an expiration date
```js
var ckpars = require("cookie-parser");
var app = require("express");
app.get("/", function(request, response){
response.cookie(name,"val",{expire: 420000 + Date.now()});//expire 7 mins from now
});

app.listen(3000);
```

### How To Delete Existing Cookie
```js
var express = require('express');
var app = express();

app.get('/clear_cookie', function(request, response){
response.clearCookie('foo');
response.send('cookie foo cleared');
});

app.listen(3000);
```
9 changes: 9 additions & 0 deletions Sessions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Sessions

- Cookies and URL are good ways to transport data in between the frontend and the backend
- The downside of cookies is that they are read on the frontend which is very dangerous
- Sessions helps us solve this security flaw and instead of outputing the credentials it gives us just an id
- This information is stored in the backend and is encrypted
- Whenever a user hits a certain endpoint it creates the session for the user and gives them a cookie
- Any proceeding visit of the user will be checked and the user is granted access and a new session is created for him
-

0 comments on commit bd1bc63

Please sign in to comment.