Code Monkey home page Code Monkey logo

cookies_intro's Introduction

Cookies

In the express docs, under 'Response', you'll find: Source: Express docs - res.cookie

The way you send a cookie with express is as part of the res object with the res.cookie method which allows us to pass in a name and a value.

app.get('/setname', (req, res)=> {
    res.cookie('name', 'stevie chicks');
    res.send(`<p>Cookie sent.</p>`)
})

Sent cookie Source: express docs

Now, to access the information stored in the cookie, we need to access it through req.cookie as express turns the data into an object contained in the request object. Unfortunately, express does not parse cookies out of the box, for that we can use cookie-parser => npm install cookie-parser.

npm cookie-parser

Signing cookies

Optionally you may enable signed cookie support by passing a secret string, which assigns req.secret so it may be used by other middleware. Source: npm - signed cookies Signing a cookie means taking a cookie and passing it through cookie-parser to 'sign it', basically encrypt it when sent to the client, then it will sent back to the server, where cookie-parser will be able to tell us if any of those cookies have been tampered with because they won't match. Signing is a way of making sure that the original data that we sent to the client through the browser is the same data that is being sent back to the server. If we want cookie-parser to parse our signed cookie, we must specify that it is signed, setting { signed: true }, in this case, as a third argument in the res.cookie response.

app.get('/getsignedcookie', (req, res) => {
    res.cookie('fruit', 'grape', { signed: true })
})

To access signed cookies, we need to look inside req.signedCookies instead of req.cookies, this is by design to show intent, to prevent malicious cookie attacks.

cookies_intro's People

Contributors

mayanhavoc avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.