NodeJS会话错误

时间:2011-07-19 00:15:23

标签: node.js

我一直收到以下错误:

/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)

以下是代码的相关部分

// 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: new MemoryStore({ reapInterval: 60000 * 10 }) }));

1 个答案:

答案 0 :(得分:2)

如果你问我,你应该提供更多信息。另外我认为你首先应该删除外部依赖(如MySQL等)。

<强>发行版

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

<强>节点

alfred@alfred-laptop:~/node/stackoverflow/6740962$ node -v
v0.4.9

<强> NPM

alfred@alfred-laptop:~/node/stackoverflow/6740962$ npm -v
1.0.15

已安装的软件包

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 

代码(基于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');

<强>卷曲

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>