1.0.1 • Published 2 years ago

cookie-v v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Introduction

Cookie utils.

Installation

npm i cookie-x

Start

const Cookie = require('cookie-v')

const privateKey = 'privateKey'

const server = http.createServer((req, res) => {
  if (req.url === '/foo') {
    const cookie = new Cookie('privateKey', req, res)

    // foo=foo;
    cookie.set({
      key: 'foo',
      value: 'foo',
    })
    
    // ['bar=bar; ', 'baz=baz.Qgcu+leCZ9dhDla9uUZXaAa1OI806hdBMQmCVC73whc=; ']
    cookie.set([
      {
        key: 'bar',
        value: 'bar',
      },
      {
        key: 'baz',
        value: 'baz',
        isSign: true
      }
    ])

    // foo=foo; max-age=3600;
    // This operation will override previous foo
    cookie.set({
      key: 'foo',
      value: 'foo',
      'max-age': '3600'
    })

    res.end()
  }

  if (req.url === '/bar') {
    const cookie = new Cookie('privateKey', req, res)

    cookie.get('foo') // foo
    cookie.get('nonexistentKey') // null

    // { foo: 'foo', bar: 'bar', nonexistentKey: null }
    cookie.get(['foo', 'bar', 'nonexistentKey'])

    res.end()
  }
})