Skip to content

Latest commit

 

History

History
29 lines (23 loc) · 703 Bytes

section3.12.md

File metadata and controls

29 lines (23 loc) · 703 Bytes

Section 3.12: Setting cookies with cookie-parser

The following is an example for setting and reading cookies using the cookie-parser module:

let express = require('express');
let cookieParser = require('cookie-parser'); // module for parsing cookies
let app = express();

app.use(cookieParser());

// Setting Cookies
app.get('/setcookie', function(req, res){
  res.cookie('username', 'Morol', { age: 24, httpOnly: true });
  return res.send('Cookie has been set');
});

// Getting Cookies
app.get('/getcookie', function(req, res) {
  let username = req.cookies['username'];
  if (username) {
    return res.send(username);
  }
  return res.send('No cookie found');
});

app.listen(3000);