Node.js 之cookie 與express 模塊樣例
/**
* Created with JetBrains WebStorm.
* User: hexie
* Date: 12-12-8
* Time: 下午9:32
* To change this template use File | Settings | File Templates.
*/
var app = require('express').createServer();
app.get('/cookie',function(req,res){
res.setHeader("Set-Cookie",["type=ninja","language=javascript"]);
res.send('cookie operation');
});
app.get('/',function(req,res){
var cookies = {};
req.headers.cookie && req.headers.cookie.split(';').forEach(function( cookie ) {
var parts = cookie.split('=');
cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim();
});
res.send(cookies['type']);//ninja
});
console.log('It works');
app.listen(80);