get an alert email when this solution is edited

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



Together We Can Simplify The World

the current number of solutions:  2072
.htaccess (1x)    AJAX (13x)    ANGULARJS (129x)    ASP (1x)    ASP.NET (4x)    BACKBONE.JS (47x)    BOOTSTRAP (12x)    C# (194x)    COFFEESCRIPT (28x)    CSS (71x)    HTML (46x)    JADE (32x)    JAVA (51x)    JAVASCRIPT (412x)    JQUERY (338x)    JQUERY MOBILE (5x)    JSON (5x)    LARAVEL (12x)    MYSQL (9x)    NODE.JS (99x)    ORACLE SQL (15x)    PERL (1x)    PHP (258x)    PYTHON (9x)    REACT (54x)    RUBY (23x)    RUBY ON RAILS (1x)    SHELL (1x)    SQL (80x)    SQL SERVER (3x)    TYPESCRIPT (47x)    VB.NET (25x)    W3.CSS (6x)    W3.JS (40x)    
join us

click to create your account

The most viewed NODE.JS solutions by country
Solution Country
detect mobile nodejsCinque Terre
How to get client ip address using node.jsCinque Terre
How to get client ip address using node.jsCinque Terre
detect mobile nodejsCinque Terre
how do I connect to mysql database using node.js ?Cinque Terre

The most helpful NODE.JS solutions
Likes Solution Technology
1.2K get user ipNODE.JS
292 how to get client netmask using node.js ?NODE.JS
251 how to get client local ip address using node.js ?NODE.JS
238 How to combine node.js and jquery ?NODE.JS
229 How to get client ip address using node.jsNODE.JS
Check out all of NODE.JS ...

set cookie nodejs

(node.js set cookie nodejs )

- NODE.JS

 viewed 19

Comment:
NODE.JS

var cookie = require('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");


    

   
created: 10 days ago

The power of the user (%)

6%

Do you know a little more effective code ? Feel free to optimize ...


Comment:
NODE.JS

// at first you install npm install cookie


    

   
created: 10 days ago

The power of the user (%)

6%

Do you know a little more effective code ? Feel free to optimize ...


haven't you found what you're looking for ?
Write us your question ...
Login is not needed.