2011-07-19 67 views
0

我不斷收到以下錯誤:會話的NodeJS錯誤

/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 }) })); 
+0

這似乎是錯誤來自模塊的代碼,而不是你自己的。無論是模塊有錯誤,或者你不知道如何正確使用它,模塊不會正常報告。 – MooGoo

回答

2

,如果你問我,你應該提供方式的詳細信息的相關部分。另外我認爲你應該首先需要刪除外部依賴(如MySQL等)。

DISTRO

[email protected]:~/node/stackoverflow/6740962$ lsb_release -a 
No LSB modules are available. 
Distributor ID: Ubuntu 
Description: Ubuntu 10.10 
Release: 10.10 
Codename: maverick 

節點

[email protected]:~/node/stackoverflow/6740962$ node -v 
v0.4.9 

NPM

[email protected]:~/node/stackoverflow/6740962$ npm -v 
1.0.15 

軟件包安裝

[email protected]:~/node/stackoverflow/6740962$ npm ls 
/home/alfred 
├── [email protected] 
├─┬ [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] extraneous 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └─┬ [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├─┬ [email protected] 
│ └── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├─┬ [email protected] 
│ └── [email protected] 
├── [email protected] 
├── [email protected] 
└─┬ [email protected] 
    ├── [email protected] extraneous 
    ├── [email protected] 
    ├── [email protected] 
    └─┬ [email protected] 
    └── [email protected] 

代碼(基於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'); 

捲曲

[email protected]:~/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>