NODEJS set cookie nodejs - Net-Raft.com

Welcome aboard! We are happy you are here and wish you good net-raft!




Just a Web Code Solution
join us

set cookie nodejs


 525

NODEJS


3

// you must install npm i cookie

var escapeHtml = require('escape-html');
var http = require('http');
var url = require('url');

function onRequest(req, res) {

var query = url.parse(req.url, true, true).query;

if (query && query.mycookie) {

// Set a new cookie
res.setHeader('Set-Cookie', cookie.serialize('name', String(query.mycookie), {
httpOnly: true,
maxAge: 60 * 60 * 24 * 7 // 1 week

}));

// Redirect back after setting cookie
res.statusCode = 302;
res.setHeader('Location', req.headers.referer || '/');
res.end();
return;

}

var cookies = cookie.parse(req.headers.cookie || '');

// Get the visitor name set in the cookie
var name = cookies.name;
res.setHeader('Content-Type', 'text/html; charset=UTF-8');

if (name) {
res.write('<p>Cookie's value is, <b>' + escapeHtml(name) + '</b>!</p>');
} else {
res.write('<p>Cookie is not set!</p>');
}
res.write('<form method="GET">');
res.write('<input placeholder="type some word" name="mycookie"> <input type="submit" value="Set cookie">');
res.end('</form');
}

http.createServer(onRequest).listen(3000);
console.log("Listening to Port 3000");


By        
The power of the user (%)
17%

NODEJS


0

// install npm install cookie-session

var cookieSession = require('cookie-session')
var express = require('express')

var app = express()

app.set('trust proxy', 1) // trust first proxy

app.use(cookieSession({
name: 'session',
keys: ['key1', 'key2']
}))

app.get('/', function (req, res, next) {
// Update views
req.session.views = (req.session.views || 0) + 1

// Write response
res.end(req.session.views + ' views')
})

app.listen(3000)


By        
The power of the user (%)
17%



The most helpful NODEJS solutions

get user ipget user ipNODEJS

Click to see more ...

  7.6K     1.7K

how to get client netmask using node.js ?how to get client netmask using node.js ?NODEJS

Click to see more ...

  4.2K     557

How to get client machine name and computer name with node.js?How to get client machine name and computer name with node.js?NODEJS

Click to see more ...

  12.1K     440

how to get client local ip address using node.js ?how to get client local ip address using node.js ?NODEJS

Click to see more ...

  2.3K     311

How to get client ip address using node.jsHow to get client ip address using node.jsNODEJS

Click to see more ...

  2.1K     271

How to combine node.js and jquery ?How to combine node.js and jquery ?NODEJS

Click to see more ...

  1.5K     247

how to detect tablet, iPad, iPhone, Android and Blackberry using nodejs ?how to detect tablet, iPad, iPhone, Android and Blackberry using nodejs ?NODEJS

Click to see more ...

  1.8K     161

get client region using node.js ?get client region using node.js ?NODEJS

Click to see more ...

  1.4K     128

get real user ip address using node.js?get real user ip address using node.js?NODEJS

Click to see more ...

  1K     124

get server ip address from hostname nodejsget server ip address from hostname nodejsNODEJS

Click to see more ...

  2.9K     122

Welcome aboard!
We are happy you are here and
wish you good net-raft!