月度归档:2013年10月

Node.js使用express解析cookie

首先使用cookieParser()解析请求头里的Cookie, 并用cookie名字的键值对形式放在 req.cookies 你也可以通过传递一个secret 字符串激活签名了的cookie 。
app.use(express.cookieParser());
app.use(express.cookieParser('some secret'));

下面是读写cookie的代码


var express = require('express');
var app = express();
app.get('/readcookie',function(req,res){
    res.send('req.cookies.name:' + req.cookies.name);
});
app.get('/writecookie',function(req,res){
    res.cookie('name', 'I'm a cookie', 60000);
    res.send("cookie saved");
});
app.listen(3000);
console.log('listening on port 3000');

Node.js教程起步资料

Tutorials

Videos

Screencasts

Books

Courses

Blogs

Podcasts

JavaScript resources

Node Modules

Other

.Net mvc自定义字符串截取(考虑全角/半角)

public static class HtmlHelpers
{
  public static string Truncate(this HtmlHelper helper, string inputString, int length)
  {
    string tempString = string.Empty;
    for (int i = 0, tempIndex = 0; i < inputString.Length; ++i, ++tempIndex)
    {
      if (System.Text.Encoding.UTF8.GetBytes(new char[] { inputString[i] }).Length > 1)
      {
        ++tempIndex;
      }
      if (tempIndex >= length)
      {
        tempString += "...";
        break;
      }
      tempString += inputString[i];
    }
    return tempString;
  }
}