Node.js 之cookie 与express 模块样例

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/a86f36dc8dd6213856efd31733698918.html