跳到主要內容

Express Error: connect.cookieParser("secret") required for security when using sessions

注意到了嗎?最近expressjs升版了,目前版本是Express 3.0.0 Alpha1

# npm list express
npm WARN ls doesn't take positional args. Try the 'search' command
/root
├── ejs@0.7.1 
├─┬ express@3.0.0alpha1 
│ ├── commander@0.5.2 
│ ├─┬ connect@2.1.2 
│ │ ├── crc@0.1.0 
....
  │ └── semver@1.0.13 
  ├── sprintf@0.1.1 
  └─┬ ssh-agent@0.1.0 
    └── ctype@0.5.0 

但是使用了新的版本,執行上卻出現不明的Exception...
仔細檢查與舊版本間的差異...發現2.x跟3.x的版本有不少地方不一樣
直覺下做了幾個測試:

1. 2.x執行3.x所產生程式碼....無法執行,3.x的一些宣告2.x不認得,應該是function有做過修改...
# node app.js 
node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
TypeError: object is not a function
    at Object.CALL_NON_FUNCTION (native)
    at Object.<anonymous> (/root/TestPrj2/app.js:10:11)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)
    at EventEmitter._tickCallback (node.js:192:40)
2. 3.x執行2.x所產生程式碼....無法執行,有下面exception...判斷是某些物件欄位修改了...
# node app.js 
node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'address'
    at Server.<anonymous> (/tmp/T1/app.js:40:69)
    at Server.g (events.js:156:14)
    at Server.emit (events.js:64:17)
    at Array.0 (net.js:777:10)
    at EventEmitter._tickCallback (node.js:192:40)
3. 3.x執行session設定....無法執行,這個我還沒找出來3.x怎麼設定session而不會有錯誤@@
# node app.js 
Error: connect.cookieParser("secret") required for security when using sessions
    at Object.session [as handle] (/tmp/T1/node_modules/express/node_modules/connect/lib/middleware/session.js:212:28)
    at next (/tmp/T1/node_modules/express/node_modules/connect/lib/proto.js:191:15)
    at Object.cookieParser [as handle] (/tmp/T1/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5)
    at next (/tmp/T1/node_modules/express/node_modules/connect/lib/proto.js:191:15)
    at multipart (/tmp/T1/node_modules/express/node_modules/connect/lib/middleware/multipart.js:45:61)
    at /tmp/T1/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:57:9
    at urlencoded (/tmp/T1/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:34:72)
    at /tmp/T1/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:55:7
    at json (/tmp/T1/node_modules/express/node_modules/connect/lib/middleware/json.js:41:55)
    at Object.bodyParser [as handle] (/tmp/T1/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:53:5)


下面是我遇到的問題....在把jade改成ejs時候還OK,但是設定session部分就會有錯誤:

使用3.0產生之原始碼:

/**
 * Module dependencies.
 */
var express = require('express')
  , routes = require('./routes')
  , http = require('http');
var app = express();
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.static(__dirname + '/public'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
});
app.configure('development', function(){
  app.use(express.errorHandler());
});
app.get('/', routes.index);
http.createServer(app).listen(3000);
console.log("Express server listening on port 3000");

修改成使用ejs做view presentation

/**
 * Module dependencies.
 */
var express = require('express')
  , routes = require('./routes')
  , http = require('http');
var app = express();
app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(express.session({ secret: "keyboard cat" }));
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
    app.set("view options", {layout : false});
})
app.get('/', function(req, res){
    res.render('index', {
      title: 'I am Benson'
    });
});
http.createServer(app).listen(3000);
console.log("Express server listening on port 3000");
比對一下2.x產生的程式碼:
/**
 * Module dependencies.
 */
var express = require('express')
  , routes = require('./routes');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
  app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.listen(3000, function(){
  console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});

2與3的差異部分大致上是紅色部分

而3.x在加上了session設定後執行錯誤如下:
[root@bensonS64PLUS1 ~/TestPrj]# node app.js                                                                                                                                                              
Express server listening on port 3000
Error: connect.cookieParser("secret") required for security when using sessions
    at Object.session [as handle] (/root/TestPrj/node_modules/express/node_modules/connect/lib/middleware/session.js:212:28)
    at next (/root/TestPrj/node_modules/express/node_modules/connect/lib/proto.js:191:15)
    at Object.cookieParser [as handle] (/root/TestPrj/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5)
    at next (/root/TestPrj/node_modules/express/node_modules/connect/lib/proto.js:191:15)
    at multipart (/root/TestPrj/node_modules/express/node_modules/connect/lib/middleware/multipart.js:45:61)
    at /root/TestPrj/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:57:9
    at urlencoded (/root/TestPrj/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:34:72)
    at /root/TestPrj/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:55:7
    at json (/root/TestPrj/node_modules/express/node_modules/connect/lib/middleware/json.js:41:55)
    at Object.bodyParser [as handle] (/root/TestPrj/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:53:5)

目前還未找到session如何使用2.x的寫法,暫時可以解決的方法如下:
1. 取消"app.use(express.session({ secret: "keyboard cat" }));"
2. 降版express:
$ npm uninstall express
$ npm uninstall express -gd
$ npm install express@2.5.9
$ npm install express@2.5.9 -gd
如果採用將版的選擇
請不要忘記修改專案package的定義
# vi package.json 
{
    "name": "application-name"
  , "version": "0.0.1"
  , "private": true
  , "dependencies": {
      "express": "2.5.8"
    , "jade": ">= 0.0.1"
  }
}

附註:
express 3.0的產生的原始碼於2.*是不能用的,專案需要重新產生
或是修改幾個部分:
1. 修改express宣告:
var app = express(); //3.0
修改為
var app = module.exports = express.createServer();
2. 修改http server建立宣告
http.createServer(app).listen(3000); //3.0
修改為
app.listen(3000, function(){
  console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});


附註:
確認您的express版本
# npm list express
npm WARN ls doesn't take positional args. Try the 'search' command
application-name@0.0.1 /tmp/T1
├── ejs@0.7.1  extraneous
├─┬ express@2.5.8 
│ ├─┬ connect@1.8.7 
│ │ └── formidable@1.0.9 
│ ├── mime@1.2.4 
│ ├── mkdirp@0.3.0 
│ └── qs@0.4.2 
└─┬ jade@0.25.0 
  ├── commander@0.5.2 
  └── mkdirp@0.3.0 








這個網誌中的熱門文章

Oracle LISTAGG

同事介紹的一個Oracle的好用查詢:LISTAGG
SELECT A.GROUP_ID,A.KEY, LISTAGG(A.VALUE,'; ')WITHINGROUP(ORDERBYA.VALUE)as GG  fromSYS_PROPERTIESaGROUP byA.GROUP_ID,A.KEY
LISTAGG可以將group後的結果會總顯示於一個欄位 上述SQL原本A.VALUE會是一個row一個row的排列 使用LISTAGG之後,可以將A.VALUE顯示在同一個row中 並且可以指定間隔符號(在此設定為';') 針對某一些報表查詢非常有用唷 :D

使用 minikube 輕鬆上手 kubernetes

安裝minikube
macOS只需要透過brew即可快速安裝...
brew cask install minikube
Linux環境可以直接下載執行檔,放到環境變數可以吃到的路徑即可...
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && \ chmod +x minikube && \
sudo mv minikube /usr/local/bin/
Windows的下載網址如下: https://storage.googleapis.com/minikube/releases/latest/minikube-windows-amd64.exe
如果您的kubectl尚未安裝,可以直接使用google cloud sdk來安裝:
curl https://sdk.cloud.google.com | bash
gcloud components install kubectl
安裝完成後,原則上minikube會在本地端加入minikube的k8s context,我們可以透過下面指令來使用該context…
kubectl config use-context minikube
然後,可檢查一下您的minikube node是否正常運作....

新一代LB - Traefik

新一代LB-Traefik Traefik突破以往我們對loadbalancer的觀點,他是一套直接與docker整合的loadbalancer套件...透過Traefik,我們可以使用label的方式將後面啟動的dockerinstance掛載到loadbalancer中,且無需重新啟動Traefik,可直接生效... Traefik基本介紹 Traefik是以動態重載新加入的dockerinstance的方式來替有附加相同domainlabel的dockerinstance建立網路附載平衡的關聯...因此,設定上,與一般我們建立reverseproxy的過程剛好相反(一般我們會先建立服務,再建立reverseproxy將服務串連起來)... Step1-建立Traefik服務 下面我們用官方的composefile來說明... File:docker-compose.yaml version: '2' services: proxy: image: traefik command: --api --docker --docker.domain=docker.localhost --logLevel=DEBUG networks: - webgateway ports: - "80:80" - "8080:8080" volumes: - /var/run/docker.sock:/var/run/docker.sock - /dev/null:/traefik.toml networks: webgateway: driver: bridge 其中traefik啟動時候,我們需要指定docker.domain來告訴taefik要聆聽的domain是哪一個,然後要事先開啟對應的port,讓外部服務可以連到traefik...,另外,我們將dockersocket掛載進來,這是必要的設定,讓traefik可以透過dockersocket來操控一些東西...,最後,traefik.toml檔案,我們保留空的,讓treafik自己建立... 啟動: docker-compose -f docker…