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

你可能感兴趣的:(JavaScript,JavaScript,JavaScript,JavaScript,Cookies,node.js)