NodeJS Session Error
I keep on getting the following error:
/usr/local/lib/node/.npm/connect/1.6.0/package/lib/middleware/session.js:263
var secured = cookie.secure && (req.connection.encrypted || req.connec
^*emphasized text*
TypeError: Cannot read property 'secure' of undefined
at ServerResponse.writeHead (/usr/local/lib/node/.npm/connect/1.6.0/package/lib/middleware/session.js:263:29)
at ServerResponse._implicitHeader (http.js:763:8)
at ServerResponse.end (http.js:628:10)
at IncomingMessage.next (/usr/local/lib/node/.npm/connect/1.6.0/package/lib/http.js:163:13)
at ServerResponse.render (/usr/local/lib/node/.npm/express/2.4.3/package/lib/view.js:327:16)
at /Users/josebalius/Sites/report/report_server.js:102:10
at /Users/josebalius/Sites/report/report_server.js:23:13
at Query.<anonymous> (/usr/local/lib/node/.npm/mysql/0.9.1/package/lib/mysql/client.js:119:11)
at Query.emit (events.js:39:17)
at Query._handlePacket (/usr/local/lib/node/.npm/mysql/0.9.1/package/lib/mysql/query.js:53:14)
Here is the related part of the code
// require express, mysql, and our very own db library
var express = require("express");
mysqlD = require("mysql").Client;
mysql = new mysqlD();
MemoryStore = require('connect').session.MemoryStore;
var app = express.createServer();
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: "hey you", store: ne开发者_C百科w MemoryStore({ reapInterval: 60000 * 10 }) }));
You should provide way more information if you ask me. Also I think you should first need to remove external dependencies(like MySQL etc).
Distro
alfred@alfred-laptop:~/node/stackoverflow/6740962$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 10.10
Release: 10.10
Codename: maverick
Node
alfred@alfred-laptop:~/node/stackoverflow/6740962$ node -v
v0.4.9
npm
alfred@alfred-laptop:~/node/stackoverflow/6740962$ npm -v
1.0.15
Packages installed
alfred@alfred-laptop:~/node/stackoverflow/6740962$ npm ls
/home/alfred
├── connect-redis@1.0.6
├─┬ everyauth@0.2.16
│ ├─┬ connect@1.6.0
│ │ ├── mime@1.2.2
│ │ └── qs@0.2.0
│ ├── connect-form@0.2.1 extraneous
│ ├── oauth@0.9.2
│ ├── openid@0.1.8
│ ├── restler@0.2.1
│ └─┬ xml2js@0.1.9
│ └── sax@0.1.5
├─┬ express@2.4.3
│ ├── connect@1.6.0
│ ├── mime@1.2.2
│ └── qs@0.2.0
├── formidable@1.0.2
├── hiredis@0.1.12
├── jade@0.13.0
├── node-expat@1.3.2
├── node-stringprep@0.0.5
├─┬ node-xmpp@0.2.9
│ └── ltx@0.0.5
├── nodeunit@0.5.1
├── notifo@0.0.2
├── openid@0.2.0
├─┬ optimist@0.2.6
│ └── wordwrap@0.0.1
├── recaptcha@1.1.0
├── redis@0.6.6
└─┬ socket.io@0.7.7
├── bison@1.1.1 extraneous
├── policyfile@0.0.3
├── redis@0.6.0
└─┬ socket.io-client@0.7.4
└── uglify-js@1.0.3
Code(based on https://raw.github.com/visionmedia/express/master/examples/session/app.js)
/**
* Module dependencies.
*/
var express = require('express'),
MemoryStore = require('express').session.MemoryStore;
var app = express.createServer(
express.logger(),
// Required by session() middleware
express.cookieParser(),
// Populates:
// - req.session
// - req.sessionStore
// - req.sessionID (or req.session.id)
express.session({ secret: 'keyboard cat', store: new MemoryStore({ reapInterval: 60000 * 10 })})
);
app.get('/', function(req, res){
var body = '';
if (req.session.views) {
++req.session.views;
} else {
req.session.views = 1;
body += '<p>First time visiting? view this page in several browsers :)</p>';
}
res.send(body + '<p>viewed <strong>' + req.session.views + '</strong> times.</p>');
});
app.listen(3000, '127.0.0.1');
console.log('Express app started on port 3000');
Curl
alfred@alfred-laptop:~/node/stackoverflow/6740962$ curl -v http://localhost:3000* About to connect() to localhost port 3000 (#0)
* Trying ::1... Connection refused
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< Content-Length: 104
< Set-Cookie: connect.sid=63UK2d71YbdrisSQbQoTBcst.ZY%2FiSqeY0g0yCEN%2FX6yUy6zmhjgi7NdPTUo9VTEMfzE; path=/; expires=Tue, 19 Jul 2011 04:29:35 GMT; httpOnly
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
* Closing connection #0
<p>First time visiting? view this page in several browsers :)</p><p>viewed <strong>1</strong> times.</p>
精彩评论