javascript - How to access header information on node js? -


how can read cookie on node js ??

var socket  = require( 'socket.io' ); var express = require('express'); var app     = express(); var server  = require('http').createserver(app); var io      = socket.listen( server ); var port    = process.env.port || 8000; var mysql = require('mysql');   function parsecookies (request) {     var list = {},         rc = request.headers.cookie;      rc && rc.split(';').foreach(function( cookie ) {         var parts = cookie.split('=');         list[parts.shift().trim()] = decodeuri(parts.join('='));     });      return list; }    http.createserver(function (request, response) {   // read cookie     var user_id= cookies.realtimeid;     console.log(user_id); });     server.listen(port, function () {   console.log('server listening @ port %d', port);   var cookies = parsecookies();   console.log(cookies);  }); 

i new on node , socket. have read cookie value set codeignter. how can send header request on parsecookies server.listen.

i see using express, suggest use known module it. cookie-parser https://www.npmjs.com/package/cookie-parser

installation

npm install cookie-parser 

how use it

var express      = require('express') var cookieparser = require('cookie-parser')  var app = express() app.use(cookieparser()) 

so after mysql require can app.use(cookieparser())

and in every request in req variable find cookies req.cookies example

var express      = require('express') var cookieparser = require('cookie-parser')  var app = express() app.use(cookieparser())  app.get('/', function(req, res) {   console.log("cookies: ", req.cookies) })  app.listen(8080) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -